ACM HDU 1004 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
 


Sample Output
  
  
red pink

题目的大致意思为给你几个颜色的单词然后需要你找出出现最多的那一种,然后把他的英文输出来;

做这道题目其实也没用到什么特别的方法我用的是容器做的;原因是你不知道他到底会输入几个单词(热气球的颜色);

但是这道题也可以用数组做因为题目说了最大为以前我们只要把数组的长度设为以前就行了;

我的思路可能有点繁琐,但容器还是一个挺管用的工具;

思路:1.把颜色种类或种类的多少记录下来(set容器)因为set里边没有重复的元素;

2.输入的每一个颜色保存起来;

3:统计每一种颜色个数(从第一个开始,第二步中的颜色等于当前的这种就加一);

4:记录最大的数,并输出;

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main()
{
	int n;
	set<string> color_kind;
	string str1;
	vector<string> color_count;
	string str;
	while(cin>>n&&n!=0)
	{
		int x=0,y=0;
		for(int i=1;i<=n;i++)
		{
			cin>>str1;
			color_kind.insert(str1);//保存种类;
			color_count.push_back(str1);//输入的每一种颜色;
		}
		vector<string>::iterator it;
		set<string>::iterator _it;
		for(_it=color_kind.begin();_it!=color_kind.end();_it++)
		{
			for(it=color_count.begin();it!=color_count.end();it++)
			{
				if(*_it==*it)
					x++;//统计每种颜色的个数;
			}
			if(x>y)
			{
				y=x;
				str=*_it;//选出最多的那种;
			}
			x=0;
		}
		cout<<str<<endl;
		color_kind.clear();//最后一定要记得清空,不然就过不了了;
		color_count.clear();
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值