Ignatius and the Princess IV——伊格納提烏斯和四公主

韓小妹發表於2018-08-09

Problem Description

"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.

Sample Input

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

Sample Output

 3

5

1

Author

Ignatius.L

題意:

問題描述

"好吧,你不是太壞,嗯...但你永遠不能通過下一個測試。"馮5166說。

"我會告訴你一個奇數n,然後n個整數。其中會有個特殊的整數,在我告訴你所有的整數之後,你必須告訴我哪個是特殊的整數。”馮5166說。

"但是特殊整數的特性是什麼?"他問。

"整數至少會出現(n+1)/2次。如果你找不到合適的整數,我會殺了公主,你也會成為我的晚餐。“馮5166說。

你能為伊格納提找到一個特殊的整數嗎?

輸入:

輸入包含幾個測試用例。每個測試用例包含兩行。第一行由奇數整數n(1<=n<=999999)組成,這表示整數的數目,馮5166將告訴我們的英雄。第二行包含n個整數。輸入在檔案結束時終止。

輸出:

對於每個測試用例,您只能輸出一行,其中包含您找到的特殊編號。

AC碼:

方法一:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000000;
int a[maxn];
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
		sort(a,a+n);
		printf("%d\n",a[n/2]);
	}
	return 0;
}

方法二:

#include<stdio.h>
#include<string.h>
int map[500001];//記錄[i]出現的次數 
int main()
{
int n,i,a,max;
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
for(i=0;i<n;i++)
{
scanf("%d",&a);
map[a]++;
if(map[a]>=(n+1)/2)
max=a;
}
printf("%d\n",max);
}
return 0;
}

 

相關文章