首页 > 代码库 > hdu 1004 map和非map写法

hdu 1004 map和非map写法

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 116867 Accepted Submission(s): 45783


Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges‘ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you. 
 

 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 

 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

 

Sample Input
5 green red blue red red 3 pink orange pink 0
 

 

Sample Output
red pink
 
用map非常简单,不过鉴于这道题本身很水,不用map也差不了太多
 
map
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<set>
 7 #include<vector>
 8 #include<map>
 9 
10 using namespace std;
11 
12 int main()
13 {
14     int n;
15     string co,ss;
16     map<string ,int>color;
17     while(cin>>n,n)
18     {
19         color.clear();
20         for(int i = 1; i <= n; i++)
21         {
22             cin>>co;
23             color[co]++;
24         }
25         int max = 0;
26         for(map<string,int>::iterator iter = color.begin();iter!=color.end();iter++)
27         {
28             if(iter->second>max)
29             {
30                 max = iter->second;
31                 ss = iter->first;
32             }
33         }
34         cout<<ss<<endl;
35 
36     }
37     return 0;
38 }

 

非map

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int n,i,j,max;
 6     char a[1010][18];
 7     while(scanf("%d",&n),n)
 8     {
 9         max=1;
10         int b[1005];
11         for(i=1;i<=n;i++)
12         {
13              scanf("%s",&a[i]);
14              b[i]=1;
15         }
16         for(i=1;i<n;i++)
17         {
18             if(b[i]==0)
19                 continue;
20             for(j=i+1;j<=n;j++)
21             {
22                 if(strcmp(a[i],a[j])==0)
23                 {
24                     b[i]++;
25                     b[j]=0;
26                 }
27             }
28         }
29         for(i=2;i<=n;i++)
30             if(b[max]<b[i])
31                 max=i;
32         printf("%s\n",a[max]);
33 
34     }
35 }

 

hdu 1004 map和非map写法