杭电1029 Ignatius and the Princess IV

Ignatius and the Princess IV


题目信息:

坏人对英雄说:我告诉你一个奇数n,然后n个数字,你必须找出其中出现次数大于(n+1)/2的数,否则我就把公主给杀掉,哈哈。

现在你昨晚英雄要解救公主;

输入:

多组输入数据,每组包括两行,第一行是一个奇数n;第二行是n个数字;

输出:

每组输入数据输出一行,为 你找出的那个数


//此方法必须要求存在一测试数据出现次数大于(n+1)/2; 
#include<stdio.h>
int main()
{
	int n,m,i,count,max;
	while(~scanf("%d",&n))
	{
		count=0;
		while(n--)
		{
			scanf("%d",&m);
			if(count==0)
			{
				max=m;
				count++;
			}
			else 
			{
				if(max==m)count++;
				else count--;
			}
		}
		printf("%d\n",max);
	}
	return 0;
}
//这个方法也要求输入数据中存在一数出现次数大于(n+1)/2; 
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000000];
int main()
{
	int n,i,j;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;++i)scanf("%d",&a[i]);
		sort(a,a+n);
		j=n/2;
		printf("%d\n",a[j]);
	}
	return 0;
}
//超时,但可以输出出现次数最多的数据,而对
//测试数据出现次数无要求 
#include<stdio.h>
#include<string.h>
int a[1000000]={0};
int main()
{
	int n,b[1001];
	while(scanf("%d",&n)!=EOF)
	{
		int m=-1,max=-1,k=0;
		memset(a,0,sizeof(a));
		for(i=0;i<n;++i)
		{
			scanf("%d",&b[i]);
			a[b[i]]++;
			if(m<b[i])m=b[i];
		}
		for(i=0;i<=m;++i)
		{
			if(a[i]>max)
			{
				max=a[i];
				k=i;
			}
		}
		printf("%d\n",k);
	}
	return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值