HDU1004

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=1004

Let the Balloon Rise

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

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
 
 
 
题目大意:找出出现次数最多的颜色。
基本思路:用一个数组存颜色,一个数组存数量,颜色相同数量加一。
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 char color[1111][20],ch[20];//color[i][]表示第i个气球的颜色是XX颜色
 7 int number[1111],n;//number[i]表示第i种颜色出现过几次
 8 
 9 int main ()
10 {
11     int i,j;
12     int ok, max, t, k;
13     while (scanf ("%d",&n)==1&&n)
14     {
15         memset(number, 0, sizeof(number));
16         memset(color, '', sizeof(color));//初始化数组
17         k = 0;
18         for (i=0; i<n; i++)
19         {
20             scanf ("%s",ch);
21             ok = 0;
22             for (j=0; j<k; j++)
23             {
24                 if (strcmp(ch, color[j])==0)//如果改颜色之前出现过,+1
25                 {
26                     number[j]++;
27                     ok = 1;
28                 }
29             }
30             if (!ok)
31             {
32                 strcpy(color[k], ch);//如果改颜色没出现过,储存起来
33                 number[k]++;
34                 k++;
35             }
36         }
37         max = 0;
38         for (i=0; i<k; i++)//找到出现次数最多的颜色
39         {
40             if (number[i] > max)
41             {
42                 max = number[i];
43                 t = i;//记录位置
44             }
45         }
46         printf ("%s
",color[t]);
47     }
48     return 0;
49 }
View Code
原文地址:https://www.cnblogs.com/dxd-success/p/4235889.html