1. 程式人生 > >Let the Balloon Rise 2周練4

Let the Balloon Rise 2周練4

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.

這題比較容易且沒什麼坑。



#include<cstring>
#include <iostream>
using namespace std;
 
int main()
{    
    int a, b,max,g=0;
    while (cin >> a&&a!=0)
    {
         char c[1100][30];
         int e[1100];
         for (int i = 0; i < 1001;i++)
             e[i]= 0;
         for (int i = 0; i < a;i++)
         {
             cin>> c[i]; g++;
         }
         for(int i=0;i<a;i++)
             for(int j=i+1;j<a;j++)
                  if(strcmp(c[i],c[j])==0)
                  e[i]++;
         max= e[0]; 
         b= 0;
         for (int i = 1; i < g+1;i++)

             if (e[i] > max) {max = e[i]; b = i; }
         cout<< c[b] << endl;
         }
    
}


[(http://acm.hdu.edu.cn/showproblem.php?pid=1004)]