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
题意:大致意思是给你一些字符串,让你找出这些字符串中出现次数最多的那个字符串,然后输出该字符串
题解:我的做法是用结构体存储字符串,然后两个for循环一个一个找每个字符串出现的次数,更新出现次数的最大值,并把出现次数最大值的字符串保存,最后输出。
代码
#include<stdio.h>
#include<string.h>
char str[20];
struct app //储存字符串
{
char s[20];
}q[1010];
int main()
{
int n,i,j,sum,max;
while(~scanf("%d",&n)&&n)
{
max=-1,sum=0;
for(i=0; i<n; i++)
scanf("%s",q[i].s);
for(i=0; i<n; i++) //找每个字符串出现的次数
{
sum=0;
for(j=i+1; j<n; j++)
{
if(strcmp(q[i].s,q[j].s)==0) //如果相等次数加一
sum++;
}
if(sum>max) //更新出现次数的最大值
{
max=sum;
strcpy(str,q[i].s); //保存字符串
}
}
printf("%s\n",str);
}
}