Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33754 Accepted Submission(s): 11367
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.
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.
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
using namespace std;
//颜色结构,num用来计算每种颜色出现的次数
struct Color{
int num;
char color[16];
};
Color input[1000] = {0,""};
//按降序排序
int int_cmp(const void *a, const void *b)
{
return *(int *)b - *(int *)a;
}
int main()
{
//freopen("E:\\in.txt","r",stdin);
int n,i,j;
char temp[16];
while(cin>>n,n!=0)
{
int count=0;
bool exist;
for(i=0; i<n; i++)
{
//每读入一个颜色,就判断是否之前出现过,如果出现过,就把exist标记为true,并把计数+1,然后立即break出循环
cin >> temp;
exist = false;
for(j=0; j<count; j++)
{
if(strcmp(temp,input[j].color) == 0)
{
exist = true;
input[j].num++;
break;
}
}
//如果不存在,就把这种颜色加到数组中去,并使计数初始为0
if(!exist)
{
//input[count].color = temp;
strcpy(input[count].color,temp);
input[count].num = 0;
count++;
}
}
//对数组进行快速排序,排在第一个的就是数量最多的颜色,最后打印
qsort(input,count,sizeof(Color),int_cmp);
cout << input[0].color << endl;
}
return 0;
}