#include <iostream> #include <string> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RANGE(i,a) for(int i=0;i<(a);++i) #define SWAP(a,b) auto c=a;a=b;b=c; int main() { int n; string colors[1001], color; int amount[1001], color_amount; cin >> n; while (n != 0){ color_amount = 0; FOR(i,0,n){ cin >> color; bool found = false; RANGE(j, color_amount){ if (colors[j] == color){ amount[j] += 1; found = true; } } if(!found){ colors[color_amount] = color; amount[color_amount] = 1; color_amount ++; } } int max = 0, max_index = 0; RANGE(i, color_amount){ if(amount[i] > max){ max_index = i; max = amount[i]; } } cout << colors[max_index] << endl; cin >> n; } return 0; }
很简单的题目,用 string 可以避免使用 strcmp 时的麻烦。