问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004
问题简述:在放气球比赛中,有很多不同颜色的气球,现在要统计哪种颜色的气球最多。
Point:N=0表示输入结束。
Get:(1)用char
定义的字符不能直接判断是否相等,要用函数strcmp(str1,str2)
。
(这个函数在头文件<string>
中)
相关链接:https://zhidao.baidu.com/question/404515625.html
(2)数字和字符串排序可以用sort
函数实现。(在此题中,用于把相同颜色排在一起)
(这个函数在头文件<algorithm>
中)
相关链接:https://blog.csdn.net/weixin_40532377/article/details/78937024
(3)用while
输入多组数据,若要实现输入0代表输入结束,在循环条件加入&&N
。
题目解析相关链接:https://blog.csdn.net/weixin_37571609/article/details/72874097
AC代码:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N,i,b[1001];
string a[1001];
while (cin>>N&&N)
{
for (i = 0; i < N; i++)
{
cin >> a[i];
}
sort(a, a + N-1);
for (i = 0; i < 1001; i++)
{
b[i] = 1;
}
int c = 0;
b[c] = 1;
for (i = 1; i < N-1; i++)
{
if (a[i]==a[i+1])
{
b[i] += b[i - 1];
}
if (b[i] > b[c])
{
c=i;
}
}
cout << a[c] << endl;
}
}