HDU 1004 Let the Balloon Rise

                                    Let the Balloon Rise

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


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
   
   
   
   
5 green red blue red red 3 pink orange pink 0
 

Sample Output
   
   
   
   
red pink
 

Author
WU, Jiazhi
 

Source
ZJCPC2004
 

Recommend
JGShining
 
 解题思路:对每种颜色进行唯一存储,建立下表对应的计数哈希表,最后查出计数数字最大的颜色的下边即可。每输入一种颜色,检查其是否存在于颜色存储数组中,若不存在则进行重新存储,并将其计数数值加一;若存在,则直接找到其颜色存储下标,并将其计数数值加一。
 
 
#include<stdio.h>  #include<string.h>  int main()  {      int n;      int i;      char ball[1000][10],color[10];  //分别存储现有不同颜色种类和暂时存储本次输入颜色      int num[1000],colors;   //分别记录每种颜色出现次数和颜色种数      while(scanf("%d",&n)&&n)      {          colors=0;   //数据初始化          memset(num,0,sizeof(num));          while(n--)          {              scanf("%s",color);              for(i=0;i<colors;i++)   //对已有颜色种类做对比                  if(!strcmp(color,ball[i]))  //匹配成功,颜色种类计数变量增加                      num[i]++;              if(i==colors)   //匹配失败,颜色种类增加,颜色存储,数量增加              {                  strcpy(ball[colors++],color);                  num[colors-1]++;              }          }          int max1=0,j;          for(i=0;i<colors;i++)   //查找已有颜色的数量最大值的下标          {              if(num[i]>max1)              {                    max1=num[i];                    j=i;              }          }          printf("%s\n",ball[j]);      }      return 0;  }  

你可能感兴趣的:(c,模拟)