杭电2025

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     string str="";
 8     string str2="";
 9     char max;
10     while (getline(cin,str))
11     {
12         str2 = "";
13         max = str[0];
14         for (int i=0; i<str.size(); i++)
15         {
16             if (str[i]>=max)
17             {
18                 max = str[i];
19             }
20         }
21 //        cout << max <<endl;
22         string add = "(max)";
23         for (int i=0; i<str.size(); i++)
24         {  
25             if (max == str[i])
26             {
27 //                str2 += (str[i]+"max");
28                 str2 += str[i];
29                 str2 += add;
30             }
31             else
32             {
33                 str2 += str[i];
34             }
35         }
36         cout << str2 <<endl;
37     }
38     return 0;
39 }

字符串初始化为空“”;每趟回归都要进行初始化;

不能对超过字符串已初始化的值得长度进行访问,否则会出现不可预料的结果。如str(i+1); i取到n的时候,str(n+1)是不可预料的;

原文地址:https://www.cnblogs.com/lingc/p/3726465.html