HDOJ-1004

Let the Balloon Rise

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

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

本题题意:首先输入数据个数n,以下n行输入n个字符串表示气球颜色,输出出现次数最多的颜色。

AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int MAX=1010;
 8 int a[MAX];
 9 char s[MAX][16];//本想用于最多15种颜色开的二维数组,后来发现一个问题
10 int n;
11
12 int main(){
13     while(scanf("%d",&n)&&n!=0){
14         for(int i=0;i<n;i++){//输入
15             scanf("%s",&s[i]);
16         }
17         memset(a,0,sizeof(a));
18         int ans=0;
19         for(int i=0;i<n-1;i++){
20             for(int j=i+1;j<n;j++){
21             if(strcmp(s[i],s[j])==0){//遇到相等的气球则a[i]++
22                 a[i]++;
23             }
24         }
25         if(a[i]>ans)
26         ans=a[i];//ans为最大
27         }
28         for(int i=0;i<n;i++){
29             if(a[i]==ans)
30             puts(s[i]);
31         }
32     }
33     return 0;
34 }

PS:

代码注释中提到的问题,s数组中的[16]看起来并没有用上,可去掉后就会出现编译问题,求大神指教[16]在这里到底做了什么。

时间: 2024-10-06 00:54:08

HDOJ-1004的相关文章

hdoj 1004 Let the Balloon Rise(模拟 || 字典树)

Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84401    Accepted Submission(s): 31831 Problem Description Contest time again

HDOJ 1004 字符串排序

题意:给出N种颜色,统计出出现次数最多的颜色. 算法:先对字符串进行排序,然后迭代的比较出出现次数最多的颜色. 代码: #include <iostream> #include <algorithm> #include <string> using namespace std; int main(){ int N,flag=0; string color[1005]; while(cin>>N&&N!=0){ int max=1,count=

HDOJ 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): 138223    Accepted Submission(s): 54591 Problem Description Contest time again! How excited it is to see balloons floating ar

HDOJ 1004 Let the Balloon Rise (字符串+stl)

题目: 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 co

HDU2607 Let the Balloon Rise II

Let the Balloon Rise II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 131    Accepted Submission(s): 37 Problem Description Contest will be end at 17:00! How excited it is to see balloons flo

模拟+思维 HDOJ 5319 Painter

题目传送门 1 /* 2 题意:刷墙,斜45度刷红色或蓝色,相交的成绿色,每次刷的是连续的一段,知道最终结果,问最少刷几次 3 模拟+思维:模拟能做,网上有更巧妙地做法,只要前一个不是一样的必然要刷一次,保证是最小的,脑洞大 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 c

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1