Let The Ballon Raise

来源:互联网 发布:淘宝支付怎么用花呗 编辑:程序博客网 时间:2024/05/29 06:42
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
 题目的意思是在每组数据中找出出现次数最多颜色,这就需要进行字符串的比较,用到strcmp函数,在数据输入的时候可以建一个二维数组,每一行存入一个字符串,代表一种颜色,然后在进行比较,在比较时创建一个一维数组,记录相同字符串出现的次数,即相同颜色的次数,最后进行比较,输出出现次数最多的字符串。
代码:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char a[1001][16];
int n,i,j,max,b[1001],t;
while(cin>>n)
{
if(n==0)break;
/
/数据为0不做处理
else 
{
for(i=0;i<n;i++)
cin>>a[i];
         for(i=0;i<n;i++)
 {
 b[i]=0;//每次需初始化,从0开始进行记录
for(j=i+1;j<n;j++)
if(strcmp(a[i],a[j])==0)//当前数组与排在它后面的数组进行比较 
b[i]+=1;//与当前颜色相同,记录数据加一
}
       //将所有的记录数据进行比较,选出最大的,输出所在字符串
max=b[0];
t=0;
for(i=0;i<n;i++)
{
if(max<b[i])
{
max=b[i];
t=i;//记录字符串所在位置
}
}
cout<<a[t]<<endl;
}
}
return 0;
}





0 0
原创粉丝点击