Num 12: HDOJ: 题目1004 : Let the Balloon Rise( 字符串问题 )

来源:互联网 发布:华为it应用技术工程师 编辑:程序博客网 时间:2024/06/05 11:50



原题链接



题目:

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 89088    Accepted Submission(s): 33718


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.
 

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
 

     需要对字符串进行处理,并记录各个字符串出现的次数,和最大数所在的字符串的位置 ( flag );


AC代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){int n,max[1001];char str[1001][100];while(scanf("%d",&n)&&n){memset(max,0,sizeof(max));int in,out;for(in=0;in<n;in++)//输入 n 个字符串;{scanf("%s",&str[in]);}for(in=0;in<n;in++)//从第一个字符串开始到最后一个字符串比较是否相同;for(out=0;out<n;out++)//每次都扫描所有的字符串;{if(strcmp((str[out]),(str[in]))==0)max[in]++;//记录每个字符串数目}int big=-1,flag;//big用于找出最大的出现次数;flag记录位置;for(in=0;in<n;in++)//寻找最大次数;{if(big<max[in]){big=max[in];flag=in;}}printf("%s\n",str[flag]);}return 0;}


0 0