hdoj 1004 寻找气球颜色出现最多次数

这是一道来自hdoj的题目,要求编写程序找出一组气球中出现次数最多的颜色。输入包含多个测试用例,每个用例有气球总数和对应颜色,输出出现次数最多的颜色。

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

<h1 style="color: rgb(26, 92, 200); text-align: center; font-family: 'Times New Roman';">让气球上升</h1><span style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;"><strong><span style="font-family: Arial; font-size: 12px; color: green;">时间限制:2000/1000(Java /其他)内存限制女士:65536/32768 K(Java /其他)
总提交(s):89080接受提交(s):33715
</span></strong></span><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">问题描述</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">比赛一次又一次! 是多么的兴奋看到气球漂浮。 但是要告诉你一个秘密,法官的最喜欢的时间是猜测最受欢迎的问题。 当比赛结束时,他们将计算每一种颜色的气球,找到结果。

今年,他们决定离开这个可爱的工作给你。
</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">输入</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">输入包含多个测试用例。 每个测试用例开始于一个数字(0 < N < = 1000),气球分布式的总数。 接下来的N行包含一个颜色。 的颜色的气球是一个字符串15小写字母。

一个测试用例与N = 0终止输入和这个测试用例不是要处理。
</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">输出</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">对于每个案例,印刷气球的颜色最受欢迎的问题在一行。 它是保证每个测试用例有一个独特的解决方案。
</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">样例输入</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: 'Courier New', Courier, monospace;">5<pre name="code" class="cpp">
绿色红色的蓝色的红色的红色的3粉红色的橙色粉红色的0

 

样例输出
红色的 粉红色的
 

 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int main()
{  
    int flag;
    int maxn;
	int n,i,j,d=0;
	char s[1001][16];
	int a[1001];
	while(scanf("%d",&n)&&n!=0)
	{
		for(i=0;i<n;i++)
		{
			scanf("%s",s[i]);
		}
		flag=0;
		maxn=0;
		for(i=0;i<n;i++)
		{   
		  	a[i]=1;  //因为最少 
			for(j=n-1;j>i;j--)//从右往左进行比较,将其出现次数不断累加 
			{  
		 		if(strcmp(s[i],s[j])==0)//类似于冒泡排序(即不重复的比较所有的数) 
				{  
			 		a[i]+=1; 
		    	}
			}
			if(maxn<a[i])
		    {
		    	maxn=a[i];//又取了一个值,让最大值永远等于所取变量 
		    	flag=i;   //找了一个标志位,不断记录出现次数最多的位数 
			}
        }  
	printf("%s\n",s[flag]);
	}
return 0;


总结:1、学会使用标志位!
2、如何寻找字符串出现次数,并进行累计,比较



                
### 冒泡排序算法中的交换次数计算 对于给定的一组数据,在HDOJ平台上的冒泡排序算法通过比较相邻元素来决定是否需要交换它们的位置。如果前面的元素大于后面的元素,则两者会互换位置,这一过程称为一次交换操作[^2]。 当涉及到统计具体的交换次数时,可以通过设置一个计数器`count`来进行记录。每当发生一次有效的交换(即两个逆序元素被调整顺序),就增加该计数器的值。最终输出这个计数值即可得到整个数组完成升序排列过程中发生的总交换次数。 具体实现方式如下所示: ```cpp #include<iostream> using namespace std; int main(){ int n; while(cin>>n){ int a[n], count=0; for(int i=0;i<n;i++) cin>>a[i]; // 开始冒泡排序并统计交换次数 for(int i=n-1;i>=0;i--){ bool flag=false; // 添加标志位用于优化 for(int j=0;j<i;j++){ if(a[j]>a[j+1]){ count++; swap(a[j],a[j+1]); flag=true; } } if(!flag) break; // 如果某一轮没有发生任何交换则提前结束循环 } cout<<count<<endl; } } ``` 上述代码不仅实现了基本功能还加入了额外的优化措施——一旦发现某一趟扫描中没有任何元素进行了交换就可以立即终止后续不必要的迭代,因为此时可以断定序列已经完全有序。 #### 应用场景 这种带有交换次数统计特性的冒泡排序通常适用于以下情况: - **教学目的**:帮助学生更好地理解和掌握基础排序原理及其内部运作机制。 - **性能测试**:评估不同初始状态下各种排序算法之间的效率差异,特别是关注实际运行期间所涉及的数据移动开销。 - **特定竞赛题型**:某些在线评测系统可能会设计专门针对此类特性的问题作为考察点之一,比如要求求解最小化或最大化某种条件下的交换总数等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值