HDU 1029 Ignatius and the Princess IV
来源:互联网 发布:自学java看什么书 编辑:程序博客网 时间:2024/06/08 04:29
HDU 1029 Ignatius and the Princess IV
Time limit: 1000 ms
Memory limit: 32767 kB
OS: Windows
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
思路
这个特殊整数出现至少(N+1)/2次,多于其他所有数的和,所以可以用ans来记录目前为止出现次数最多的数,用num记录其出现次数,如果下一个数不是ans,num就-1,否则num+1,N个数输入完后,数量依然为正数的数就是出现最多的数,也就是ans
Submit
#include <cstdio>using namespace std;int main(){ int N, n, i; while(~scanf("%d", &N)) { int ans = -1, num = 0; for(i = 0; i < N; i++) { scanf("%d", &n); if(num == 0) { num++; ans = n; } else { if(ans != n) num--; else num++; } } printf("%d\n", ans); } return 0;}
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- Ignatius and the Princess IV hdu 1029
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU-1029-Ignatius and the Princess IV
- HDU--1029--Ignatius and the Princess IV
- hdu - 1029 -Ignatius and the Princess IV
- 求二叉树的先序遍历
- 最短路判负环uva11090
- 01背包
- css3实现点击切换背景图片,并且背景图片实现动画效果
- 线程
- HDU 1029 Ignatius and the Princess IV
- 恢复旋转排序数组
- 如何获取本机IP地址
- 设计模式读书笔记-----简单工厂模式
- 单例模式与多线程(一)
- SEM的关键词规则、匹配模式、出价
- 按钮设置
- POJ 3270 Cow Sorting(置换最小费用)
- adreno-profiler 3-7