寒假练习2_A 涂气球颜色

来源:互联网 发布:自己淘宝旺旺号是什么 编辑:程序博客网 时间:2024/05/01 07:02

需要用到字符串组

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.

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

5greenredblueredred3pinkorangepink0

SAMPLE OUTPUT

redpink



#include<stdio.h>#include<string.h>int main(){int n,i,j,num,max;int a[1005],b[1005];char str[1005][16];    //前面括号是多少组字符串,后面是指每个字符串含有多少个字符while(scanf("%d",&n)!=EOF){if(n==0)break;memset(a,0,sizeof(a));memset(b,0,sizeof(b));max=0;for(i=1;i<=n;i++){scanf("%s",str[i]);}for(i=1;i<=n;i++){if(b[i]==1) continue;   //标记是否出现过,之前出现过就跳过,减少时间复杂度for(j=i;j<=n;j++){if(strcmp(str[i],str[j])==0){b[j]=1;a[i]++;}}if(a[i]>max){max=a[i];  //记录当前出现最多的颜色num=i;}}printf("%s\n",str[num]);}return 0;}

0 0
原创粉丝点击