本文主要是介绍ACM DP Ignatius and the Princess IV,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
"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?
OutputFor 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 1Sample Output
3 5 1
题意:
给n个数,求出出现(n + 1)/ 2次的那个数
分析:
排序,取中间值
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
int a,b[1000001];
int main()
{
while(scanf("%d",&a)!=EOF)
{
for(int i=0;i<a;i++)
scanf("%d",&b[i]);
sort(b,b+a);
printf("%d\n",b[(a+1)/2]);
}
return 0;
}
这篇关于ACM DP Ignatius and the Princess IV的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!