http://acm.hdu.edu.cn/showPRoblem.php?pid=1004
利用map容器可以很容易的解決,同樣也可以考慮結構體或者二維數組的方法,有興趣的可以自己實現一下。
#include<iostream>#include<map>using namespace std;int main(){ int n,max; map<string , int> ballon; string color; string MaxColor; while(cin>>n) { if(n==0) { return 0; } ballon.clear(); for(int i=0;i<n;i++) { cin>>color; ballon[color]++; } map<string, int>::iterator it; max=0; for(it=ballon.begin();it!=ballon.end();it++) { if(it->second>max) { max = it->second; MaxColor = it->first; } } cout<<MaxColor<<endl; }}注意:記得每次進行操作之前先把容器清空,否則上次運行的結果會影響下一次的運算。
新聞熱點
疑難解答