您的位置:首页 > 其它

杭电 1004题 Let the Balloon Rise

2018-02-05 00:24 477 查看
Let the Balloon Rise

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

解决代码如下:

#include<iostream>
using namespace std;
int main () {
char str[1000][20];   //该数组存储颜色值
char s[20];         //临时数组,存放从缓冲区取的值,经判断后再决定是否加入到str数组中
int a[1000];        //与str数组同步,判断每种颜色的数目
int m,n;            //m临时变量,n决定接下来要输入几种颜色或者是否结束输入
int k,j;            //临时变量

char color[20];     //存放出现次数最多的颜色
int N;              //存放出现次数最多的颜色的数目

cin>>n;
while(n!=0)
{
//辅助数组初始化
for(int i=0;i<1000;i++)
{
a[i]=0;
}

N=-1;k=0;

//输入数据,同时记录相同元素个数
for(int i=0;i<n;i++)
{
cin>>s;
//和之前元素进行比较,想同,与之对应的元素个数+1
for(j=0;j<i;j++)
{
if(strcmp(str[j],s)==0)
{
a[j]++;
if(N<a[j])
{
strcpy(color,s);
N=a[j];
}
break;
}
}

//该比较包含两层意思,1、i=j=0时,第一个值为str[0] 2、当该颜色与之前颜色都不同时,该颜色的值加入到数组中
if(j==i)
{
strcpy(str[k],s);
a[k]++;
if(N<a[k])
{
strcpy(color,s);
N=a[k];
}
k++;
}
}
//输出结果
m=0;
while(color[m]!='\0')
{
cout<<color[m];
m++;
}
cout<<endl;

//输入下一个数据,决定是否继续输入
cin>>n;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: