Let the Balloon Rise

B - Let the Balloon Rise
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit   Status   Practice   HDU 1004

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
代码:

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int n;
    char s[1005][100];
    int a[1005];
    while(cin>>n&&n)
    {
        int i,j,k;
        int sum=0;
        memset(a,1,sizeof(a));
        for(i=0; i<n; i++)
            cin>>s[i];
        int maxx=0;
        for(i=0; i<n; i++)
        {int x=i;
            for(j=i+1; j<n; j++)
            {
                if(strcmp(s[i],s[j])==0)
                {
                    a[x]++;
                    if(a[x]>maxx)

                    {
                        maxx=a[i];
                        k=x;
                    }
                }

            }
        }
           cout<<s[k]<<endl;

    }
    return 0;
}


map代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <cstdio>
using namespace std;
int main()
{int n;

while(cin>>n&&n)
{
    map<string ,int >p;
p.clear();
char a[100];
int i;
int num;
for(i=0;i<n;i++)
{
    scanf("%s",a);
    p[a]++;
}
map <string ,int >::iterator it,maxx;
maxx=p.begin();
for(it=p.begin();it!=p.end();it++)
{
    if(it->second>maxx->second)
    {
        maxx=it;
    }
}
cout<<maxx->first<<endl;

}
    return 0;
}
这个map的代码采用了迭代访问,也可以不采用迭代访问,直接进行
比较即可代码如下:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
using namespace std;
int main()
{int n;
while(cin>>n&&n)
{map<string ,int >m;
    int i;
    char a[100];
    int maxx=0;
    string q;
    for(i=0;i<n;i++)
    {
        scanf("%s",a);
        string s=a;
        m[s]++;
        if(m[s]>maxx)
        {
            maxx=m[s];
            q=s;
        }


    }
    cout<<q<<endl;
}
    return 0;
}



你可能感兴趣的:(Let the Balloon Rise)