Ignatius and the Princess IV
来源:互联网 发布:hd电视直播软件 编辑:程序博客网 时间:2024/05/25 19:55
Ignatius and the Princess IV
"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?
InputThe 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.
OutputFor each test case, you have to output only one line which contains the special number you have found.
Sample Input5
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 Output3
5
1
#include <iostream>#include<cstdio>#include<cstring>#include <cstdlib>#include<algorithm>using namespace std;int a[1000000];int cmp(int x,int y){ return x>y;}int main(){ int n,i,sum,j,scnt; while(scanf("%d",&n)!=EOF) { j =sum= 0; for(i = 0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n,cmp); scnt= a[0]; for(i=0;i<n;i++) { if(a[i]==scnt) sum++; else if(a[i]!=scnt) { if(sum>=(n+1)/2) { break; } sum=0; scnt=a[i]; } } printf("%d\n",scnt); } return 0;}
0 0
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- 抽象类和抽象方法
- ajax方法
- GAN框架研究与思路整理
- 笔试小问题整理
- GitBook使用教程
- Ignatius and the Princess IV
- numpy:将数组保存到txt文件中
- MySQL创建用户与授权方法
- 编写聊天室
- iOS源码解析—YYCache(YYMemoryCache)
- 接口与多态
- test
- 深入理解 Java 垃圾回收机制
- HOG:用于人体检测的梯度方向直方图 Histograms of Oriented Gradients for Human Detection