HDU 1004 Let the Balloon Rise
http://acm.hdu.edu.cn/showproblem.php?pid=1004
题意:有n个气球,找出出现次数最多的颜色。
题解:练习map……其实用strcmp()也可以。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <list> 9 #include <map> 10 #include <queue> 11 #include <stack> 12 #include <bitset> 13 #include <algorithm> 14 #include <numeric> 15 #include <functional> 16 #include <set> 17 #include <fstream> 18 19 using namespace std; 20 21 int main() 22 { 23 map<string, int> bal; 24 map<string, int>::iterator ite; 25 int n; 26 string s; 27 while ((scanf("%d",&n))!=EOF&&n!=0) { 28 int maxn=-1; 29 bal.clear(); 30 for (int i=0; i<n; i++) { 31 cin>>s; 32 bal[s]++; 33 } 34 for (ite=bal.begin(); ite!=bal.end(); ite++) { 35 if (maxn<ite->second) { 36 maxn=ite->second; 37 } 38 } 39 for (ite=bal.begin(); ite!=bal.end(); ite++) { 40 if (maxn==ite->second) { 41 cout<<ite->first<<endl; 42 } 43 } 44 } 45 return 0; 46 }
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <list> 9 #include <map> 10 #include <queue> 11 #include <stack> 12 #include <bitset> 13 #include <algorithm> 14 #include <numeric> 15 #include <functional> 16 #include <set> 17 #include <fstream> 18 19 using namespace std; 20 21 const int maxn=1010; 22 23 int main() 24 { 25 int n; 26 char s[maxn][20]; 27 int nu[maxn]; 28 while (scanf("%d",&n)!=EOF&&n!=0) { 29 memset(nu, 0, sizeof(nu)); 30 int max=0; 31 for (int i=0; i<n; i++) { 32 scanf("%s",s[i]); 33 for (int j=0; j<i; j++) { 34 if (strcmp(s[i], s[j])==0) { 35 nu[i]++; 36 } 37 } 38 } 39 int t=0; 40 for (int i=0; i<n; i++) { 41 if (max<nu[i]) { 42 max=nu[i]; 43 t=i; 44 } 45 } 46 printf("%s\n",s[t]); 47 } 48 return 0; 49 }