HDU2025 查找最大元素

【题目】


【思路】用字符数组读取字符串,设置一个max用于记录最大字母,初始化为'\0'。然后遍历字符串,在输出最大字母后面插入(max)。

【代码】AC的C++代码如下:

#include <iostream>
#include <string.h>
using namespace std;
char s[1000];
int main()
{
    while (cin >> s)
    {
        char max = '\0';
        int len = strlen(s);

        for (int i = 0;i < len;i++)
        {
            if (s[i] > max)
                max = s[i];
        }

        for (int i = 0;i < len;i++)
        {
            cout << s[i];
            if (s[i] == max)
                cout << "(max)";
        }
        cout << endl;
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/m0_38056893/article/details/80055057