This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
5 green red blue red red 3 pink orange pink 0
redpink
思路:将输入的颜色存放到color缓存,与color_table比较,有相同的,则将该颜色的计数值加一,没有则存放到color_table表中,颜色输入结束,输出颜色
计数值最大的颜色。
#include<iostream> #include<string> using namespace std; int main() { char color_table[1000][15] //存放颜色 int count[1000], total; char color[15]; int i,j,flag,n, popular; while (cin>>n&&n!=0) { total = 0; memset(count, 0, 1000); for (i = 0; i < n;i++) { cin >> color; flag = 0; for (j = 0; j < i; j++) { if (strcmp(color_table[j], color) == 0)//如果输入的颜色与表中的某一颜色相同
{ count[j]++; //该颜色个数+1 flag = 1; } } if (flag == 0) { strcpy(color_table[total], color); count[i]++; total++; } } popular = 0; for (i = 0; i<total;i++){ if (count[popular]< count[i]) { popular = i; } } cout << color_table[popular] << endl; } return 0; }