当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 找出该字符串中出现次数最多的那个字符

找出该字符串中出现次数最多的那个字符

2018年03月08日  | 移动技术网IT编程  | 我要评论

图114,灵游记私服,evernote

/*
时间限制 C/C++ 3s 其他 6s, 空间限制 C/C++ 32768k 其他 65535k

题目描述
    给定一个长度不限的字符串,请找出该字符串中出现次数最多的那个字符,并打印出该字符及其出现次数;
 如果多个字符的出 现次数相同,只打印首个字符;输出字符的大小写格式要与输 入保持一致,大小写不敏感模式下,
 输出字符的大小写格式与该 字符首次出现时的大小写格式一致。实现时无需考虑非法输。

输入描述
    输入为 字符串大小写敏感标记 其中"大小写敏感标记"为可选参数,取值范围为七yue|1fa1 se,txue表示大小写敏感;缺省取值txue
 例子: abcdabcde fa1e

输出描述
    输出:字符出现次数 如果出现次数最多的字符有多个,输出字典序最小的那个字 符。输出的字符都为小写字符
 例子: a 2
*/

C++实现

#include<iostream>
using namespace std;


int main()
{
    char c[60000] = { 0 };
    char s[5] = { 0 };
    unsigned int count[52] = { 0 };
    char output[52] = { 0 };
    memset(c, 0, 60000);
    bool sensitive = true;
    cin >> c;
    cin >> s;
    int i = 0;
    int index = 0;
    while ((i < 5) && (s[i] != 0))
    {
        if (strcmp(&s[i + 1], "true"))
        {
            sensitive = true;
        }
        else
        {
            sensitive = false;
        }
        break;

        i++;
    }
    int j = 0;
    int max = 0, outputIndex = 0;
    while ((j < 6000)&&(c[j]!=0))
    {
        if (sensitive)
        {
            if (90 < c[j])
            {
                index = c[j] - 'A';
                count[index]++;
                if (output[index] == 0)
                {
                    output[index] = c[j];
                }
                if (max < count[index])
                {
                    max = count[index];
                    outputIndex = index;
                }
            }
            else
            {
                index = c[j] - 'a';
                count[index + 26]++;
                if (output[index + 26] == 0)
                {
                    output[index + 26] = c[j];
                }
                if (max < count[index + 26])
                {
                    max = count[index + 26];
                    outputIndex = index + 26;
                }
            }
        }
        else
        {
            if (90 < c[j])
            {
                index = c[j] - 'A';
            }
            else
            {
                index = c[j] - 'a';
            }
            count[index]++;
            if (output[index] == 0)
            {
                output[index] = c[j];
            }
            if (max < count[index])
            {
                max = count[index];
                outputIndex = index;
            }
        }
        j++;
    }
    cout << output[outputIndex] << " " << max << endl;
    system("pause");
    return 0;
}

 

注意编译器不兼容的问题

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网