HDU-1004 Let the Balloon Rise(字符串或者STL)

本文介绍了一种算法竞赛问题,即统计最热门问题所对应的气球颜色。提供了两种解题思路及代码实现,一种使用字符串数组结合遍历计数,另一种利用C++ STL中的map容器简化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Let the Balloon Rise

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


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

    使用字符串解题思路:

    将字符串储存在字符串数组中,遍历查询以前输入的所有字符串,并且对每一个字符串所对应的数组进行计数,定义一个数,      找出出现次数最多的字符串的下标,输出

    AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[1005];char b[10005][30];//a是计数数组,如果使用标记数组,尽量不要用int

int main()
{
	int m;
	while(cin >> m && m!= 0)
	{
		memset(a,0,1005);
		cin >> b[0];
		for(int i = 1;i < m;i++ )
		   cin >> b[i];
        for(int i = 1;i < m;i++)
        {
        	for(int j = 0;j < i -1;j++)
        	{
        		if((strcmp(b[i],b[j]) == 0))
        		a[i]+=1;
			}
		}
		int mx = a[0],t = 0;
		for(int i = 1;i < m;i++)
		{
			if(mx < a[i])
			{
				mx = a[i];//这里一定要更新mx的值
				t = i;
			}
		}
		cout << b[t] << endl;
	}

     使用STL解题思路(map容器):

      (先介绍下map,毕竟楼主也刚学c++)map映照容器的元素是由一个键值和一个映照数据组成的,键值和映照数据之间有           一一映照的关系。map映照容器的数据结构也是采用红黑树来实现的,插入元素的键值不允许重复,比较函数只对元素的键值进行比较,元素的各项数据可以通过键值检索出来。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm> 
#include <map>

using namespace std;

int main()
{
	int m;	
	map<string,int>ma;//定义map容器
	while(cin >> m && m != 0)
	{
		string a;
		for(int i = 0;i < m;i++)
		{
			cin >> a;
			ma[a]++;//映照数据计数
		}
		map<string,int>::iterator it;//定义前向迭代器
		int manx = -230;string  mx;
		for(it = ma.begin();it != ma.end();it++)//遍历开始到结束
		{
			if((*it).second > manx)
			{
			  manx = (*it).second;//一定更新manx的值
			  mx = (*it).first;				
			}

		}
		cout << mx <<endl;
		ma.clear();//清空map容器ma
	}
	return 0;
 } 



     



期末大作业基于python的足球运动员数据分析源码+数据集(高分项目),个人经导师指导并认可通过的高分设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在做大作业、毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于python的足球运动员数据分析源码+数据集(高分项目)期末大作业基于pyth
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值