1004 -Let the Balloon Rise
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
Author
WU, Jiazhi
Source
Recommend
JGShining
解题思路:每输入一次就将其与原有的所有字符串匹配,若发现是原来有的,则将对应的计算次数的数组的加一,若发现他是原来没有的,则在原有的数组后面插入一个新的字符串。最后再找记录次数的那个数组里面最大的数的下标,再输出对应的位置的字符串即可。
AC代码:
#include<stdio.h>
#include<string.h>
int num[1005];
char str[1005][20],a[20];
int main() {
int n;
while(scanf("%d",&n)&&n!=0){
memset(num,0,sizeof(num));
int j=0;
for(int i=1;i<=n;++i) {
getchar();
scanf("%s",a);
int biaoji=0;
for(int k=1;k<=j;++k)
if(!strcmp(a,str[k])) {
biaoji=1;
num[k]++;
break;
}
if(biaoji==0) {
j++;
strcpy(str[j],a);
}
}
int max=1;
for(int i=1;i<=j;++i)
if(num[i]>num[max])
max=i;
printf("%s\n",str[max]);
}
return 0;
}
#include<string.h>
int num[1005];
char str[1005][20],a[20];
int main() {
int n;
while(scanf("%d",&n)&&n!=0){
memset(num,0,sizeof(num));
int j=0;
for(int i=1;i<=n;++i) {
getchar();
scanf("%s",a);
int biaoji=0;
for(int k=1;k<=j;++k)
if(!strcmp(a,str[k])) {
biaoji=1;
num[k]++;
break;
}
if(biaoji==0) {
j++;
strcpy(str[j],a);
}
}
int max=1;
for(int i=1;i<=j;++i)
if(num[i]>num[max])
max=i;
printf("%s\n",str[max]);
}
return 0;
}

浙公网安备 33010602011771号