hdu Let the Balloon Rise

Let the Balloon Rise

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 63   Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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

题目大意:输入包含多个测试案例。每个测试案例始于一个数N0n<=1000)。

接下来的N行每行包含一种颜色。

气球的颜色是一个字符串,最多15个小写字母。

N = 0时结束。

输出出现次数最多的字符窜。

注意:应考虑字符窜出现相同次数的情况

代码:

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
    long long int i,j,max,n,s1,b[1002]={0};
    string string[1001]={},temp;        //注意头文件<string>
    while((cin>>n)&&n!=0)           //多组数据的输入注意结束标志
    {
        for(i=0;i<n;i++)             //输入n个字符窜
        cin>>string[i];
         //累计相同字符窜出现的次数
        for(i=0;i<n;i++)           
        {
            s1=1;                  //每个字符串计数时都保证初值为1
            if(string[i]!="l")          //如果字符串没有和前面的任一个相同时进行寻找//与其相同的字符串并计数
            {
                for(j=i+1;j<n+1;j++)
                {
                    if(string[j]==string[i]) //如果出现与其相同的字符串就赋值为l并计数
                    {
                      string[j]="l";
                      s1++;
                    }

                }
                b[i]=s1;               //把第i个字符串的个数赋值给b[i]
            }
        }
        //对数组b按从大到小排序交换b[i]时一定把string[i]同时交换,保证一致性
        int t;
        for(i=0;i<n-1;i++)
        {
            max=i;
            for(j=i+1;j<n;j++)
            if(b[j]>b[max])
            max=j;
            t=b[i];
            b[i]=b[max];
            b[max]=t;
            temp=string[i];
            string[i]=string[max];
            string[max]=temp;
        }
        //把出现次数最多的字符串输出
        for(i=0;i<n;i++) 
            if(b[i]!=b[i+1])     //如果出现相同最高次数的当次数与下一个不相同时
            {
                for(j=0;j<=i;j++) //从第一个到与下一个不相同时依次输出string[j]
                cout<<string[j]<<endl;
                break;
            }
    }
    return 0;
}


 

自我感觉代码不简便,望大神们指教。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值