Ignatius and the Princess IV(stl&数组)

Ignatius and the Princess IV HDU - 1029
“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.

“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.

“But what is the characteristic of the special integer?” Ignatius asks.

“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…” feng5166 says.

Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
"好吧,你还不算太坏,他们…但你永远无法通过下一次测试。
"我会告诉你一个奇怪的数字N,然后N整数。他们中间会有一个特别的整数,在我告诉你所有的整数之后,你必须告诉我哪个整数是特别的。
"但是特殊整数的特点是什么?伊格纳修斯问道。
"整数将至少出现(N+1)/2次。如果你找不到合适的整数, 我会杀了公主, 你也会是我的晚餐。哈哈哈…"冯5166说。
你能找到伊格纳修斯的特殊整数吗?
输入
输入包含多个测试案例。每个测试案例包含两行。第一行由一个奇怪的整数 N (1<=N<=999999) 组成,表示整数风 5166 将告诉我们的英雄。第二行包含 N 整数。输入在文件结束时终止。
输出
对于每个测试案例,您只需要输出一条包含您找到的特殊号码的行。
示例输入
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
样品输出
3
5
1
题意描述:给多组数据,输出每组数据里出现次数在这组数+1除2以上的数
解题思路:可以利用vector存数组,然后利用count函数计算数据出现的次数,然后输出次数大于(n+1)/2的即可,或者利用两个数组写,具体看代码
AC1

#include<stdio.h>
#include<vector>
#include<algorithm>
#define N 1010000
using namespace std;
int p[N],f[N];
int main(void)
{
	int n;
	vector<int>a;//定义数组 
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&p[i]);
			a.push_back(p[i]);//尾部插入数组
		}
		for(int i=1;i<=n;i++)
		{
			//count函数用来统计字符串中某个字符的个数
			f[i]=count(a.begin(),a.end(),p[i]);//起始地址,结束地址,需要查找的字符
			if(f[i]>=(n+1)/2)
			{
				printf("%d\n",p[i]);
				break;
			}
		 } 
		 a.clear();
	}
	return 0;
}

AC2

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1010000
using namespace std;
int a[N],b[N];
int main(void)
{
	int n,m;
	while(~scanf("%d",&n))
	{
		memset(b,0,sizeof(b));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		m=(n+1)/2;
		for(int i=1;i<=n;i++)
		{
			b[a[i]]++;
			if(b[a[i]]>=m)
			{
				printf("%d\n",a[i]);
				break; 
			 } 
				
		}
	}
	return 0;
 } 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值