第二期訓練第四題(HDU-1004)
阿新 • • 發佈:2018-12-22
問題連結: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;
}
}