这道题初看觉得很简单,但是综合很多特性,虽然用的方法比较笨,但是成功做出来了。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
struct Color{
int count = 0;
string color;
};
Color colors[1000] = {};
int main(){
int n;
while(cin>>n && n!= 0){
for(int i=0;i<n;i++){
cin>>colors[i].color;
colors[i].count= 0;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(strcmp(colors[i].color.data(),colors[j].color.data()) == 0){
colors[i].count++;
}
}
}
int max = colors[0].count;
int tmp = 0;
for(int i=0;i<n;i++) {
if (colors[i].count > max) {
max =colors[i].count;
tmp = i;
}
}
cout<<colors[tmp].color<<endl;
}
return 0;
}
写出来之后参考大佬的AC方式,比我写的优雅多了。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <set>
#include <sstream>
#include <vector>
#include<map>
using namespace std;
map<string,int> colors;
int main(){
int n;
while(cin>>n && n!= 0){
colors.clear();
int max = 0;
string key,p;
for(int i=0;i<n;i++){
cin>>key;
colors[key]++;
if(colors[key] > max){
max = colors[key];
p = key;
}
}
cout<< p <<endl;
}
return 0;
}