B - Let the Balloon Rise
题目
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
题目来源:hdu1004.
代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a,b=0,n;
char mostball[1001][15];
scanf("%d",&a);
while(a)
{
int num=0,max=0;
char ball[1001][15];//申请空间略多于题目要求即可
int color[1001]={0};
while(num<a)
{
scanf("%s",ball[num]);
num++;
}
for(num=0;num<a-1;num++)
{
for(n=num+1;n<a-1;n++)
if(strcmp(ball[num],ball[n])==0)//从第一个字符串开始,遇到相同字符串(相同颜色气球),给color[num]加1
color[num]++;
}
for(n=0;n<a;n++)
{
if(color[max]<color[n])//遍历color数组找到颜色最多的下标
max=n;
}
for(n=0;ball[max][n]!='\0';n++)
mostball[b][n]=ball[max][n];
mostball[b][n]='\0';
b++;
scanf("%d",&a);
}
for(n=0;n<b;n++)
{
printf("%s\n",mostball[n]);
}
return 0;
}
博主刚刚起步,能力有限。欢迎随时交流指正。