阅读652 返回首页    go 阿里云 go 技术社区[云栖]


字母统计

字母统计

时间限制:3000 ms  |  内存限制:65535 KB
难度:1
描述
现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小的那个。
输入
第一行输入一个正整数T(0<T<25)
随后T行输入一个字符串s,s长度小于1010。
输出
每组数据输出占一行,输出出现次数最多的字符;
样例输入
3
abcd
bbaa
jsdhfjkshdfjksahdfjkhsajkf
样例输出
a
a
j

思路是:先找出出现最大值,然后再循环找出最小字母出现最多次数的下标,就可以了! 

查看代码---运行号:252183----结果:Accepted

运行时间:2012-10-05 12:15:42  |  运行人:huangyibiao
01.#include <iostream>
02.#include <algorithm>
03.#include <cstring>
04.using namespace std;
05. 
06.int main()
07.{
08.int testNum;
09.cin >> testNum;
10.cin.get();
11.int ch[26], c;
12.while (testNum--)
13.{
14.memset(ch, 0, sizeof(ch));
15.while ((c = cin.get()) != '\n')
16.{
17.ch[c - 'a']++;
18.}
19.int max = ch[0];
20.for (int i = 1; i < 26; i++)
21.{
22.if (max < ch[i])
23.{
24.max = ch[i];
25.}
26.}
27.//cout << "max:" << max << endl;
28.int index = 0;
29.for (int i = 0; i < 26; i++)
30.{
31.if (ch[i] == max)
32.{
33.index = i;
34.break;
35.}
36.}
37.cout << char(index + 'a') << endl;
38.}
39.return 0;
40.}

最后更新:2017-04-02 15:14:53

  上一篇:go 其实,你不懂代码
  下一篇:go 在 JavaScript 中创建 JSON 对象