原题描述:
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
一开始很天真的想直接把颜色做下标。。累加排最大值就好了。。。但是字母字符串变为数字???好吧不现实。。。
但还是可以用数组来记录,color[下标][颜色]记录每个输入过得颜色,count[下标]记录数量
#include <stdio.h>
#include <string.h>
int main ( )
{
int N , i , j , count[ 1001 ] ;
char color[ 1001 ] [ 20 ] ;
while ( (scanf("%d",&N ) ) != EOF && N )
{
for ( i = 0 ; i < N ; i ++ )
{
scanf("%s",color[ i ] ) ;
for ( j = 0 ; j < i ; j ++ )
if ( strcmp ( color[ i ] , color[ j ] ) == 0 )
count[ i ] ++ ;
}
int max = 0 , index = 0 ;
for ( i = 0 ; i < N ; i ++ )
if ( count[ i ] > max )
{
max = count[ i ] ;
index = i ;
}
printf("%s\n",color[ index ] );
}
return 0;
}
其实最主要的就是怎么把相同的颜色放在一起计数,这里用的是将输入的这个颜色字符串和之前所有的作比较。
解决比赛中气球颜色统计的问题,通过记录每种颜色出现的次数并找出最多的一种。
289

被折叠的 条评论
为什么被折叠?



