您的位置:首页 > 其它

HDU 1029 基础dp

2016-02-11 22:03 357 查看
题目链接:Ignatius and the Princess IV

大意:就是在N个数里找出唯一一个至少出现过(N+1)/ 2 次的数。 1 <= N <= 999999。

hash:

/*
get到一个很巧妙的方法。按照题意。我求的那个数是素有数里出现的次数最多的,
每次只拿当前的数和前面的所有数里出现次数最多的数比较。
从过程来看?就是这道题的动态规划?
*/

#include <stdio.h>
#include <string.h>
#include <string.h>
#include <iostream>
using namespace std;

int main() {
int n;
int num, cnt, rem;
while(~scanf("%d", &n)) {
cnt = 0;
for (int i=0; i<n; ++i) {
scanf("%d", &num);
if (cnt == 0) { // 计数
rem = num;
cnt = 1;
}
else if (num == rem) {
cnt++;
}
else {
cnt--;
}
}
printf("%d\n", rem);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: