Description
给定一个只有小写英文字母组成的字符串,串长为n。请你编写程序求出这个字符串中出现次数最多的字母。
Input
输入的第一行为t(0 < t < 10),表示有t组测试用例。
对于每组测试用例,输入有两行。
第一行是一个正整数n( 1 < n < 100)表示字符串的长度。
后面一行是一个长度为n的字符串(只由小写字母组成)。
Output
对于每组测试用例,输出一行,仅输出出现次数最多的字母。
测试用例保证出现次数最多的字母只有一个。
Sample Input
2
5
acmcs
3
zzt
Sample Output
c
z
我的代码如下:
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
int a[26]={0},b,c,max,t,i;
char s[1024];
char re;
string aa;
scanf("%d",&b);
while(b--)
{
max=0;
scanf("%d",&c);
re=getchar();
gets(s);
aa=s;
for(i=0;i<c;i++)
{
(a[aa[i]-'a'])++;
if(max<a[aa[i]-'a']){max=a[aa[i]-'a'];t=aa[i]-'a';}
}
cout<<(char('a'+t))<<endl;
for(i=0;i<26;i++)
{
a[i]=0;
}
}
// system("pause");
}