Let the Balloon Rise 1004

来源:互联网 发布:python 库安装 编辑:程序博客网 时间:2024/06/05 12:02


Let the Balloon Rise

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


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
5greenredblueredred3pinkorangepink0
 

Sample Output
redpink
 

Author
WU, Jiazhi
 

Source
ZJCPC2004
 

Recommend
JGShining

用容器map一下就解决了


#include<stdio.h>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
const int MAXN=1000;
int main()
{
    map<string,int>color;
    string cnt;
    string index;
    int i;
    int n;
    int max;
    while(scanf("%d",&n),n)
    {    
       max=0;
        while(n--)
        {
        
            cin>>cnt;
            color[cnt]++;
            if(color[cnt]>max)
            {
            index=cnt;
            max=color[cnt];
            }
            
        }
      cout<<index<<endl;
    }
    return 0;   
}



不用容器的代码:

#include<iostream>
#include<cstring>
using namespace std;
struct node
{
    char s[16];
    int x;
}a1[1000],a2[1000];
int main()
{
    int n,i,j;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        int k=0,m=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",a1[i].s);
            a1[i].x=1;    
            strcpy(a2[i].s,a1[i].s);
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(!strcmp(a1[i].s,a2[j].s))
                a1[i].x++;
            }
        }
            for(i=0;i<n;i++)
            {
                if(a1[i].x>m){
                    m=a1[i].x;
                    k=i;
                 }
             }
            printf("%s\n",a1[k].s);
    }
    return 0;
}



原创粉丝点击