HDOJ 1029:Ignatius and the Princess IV 解题报告

来源:互联网 发布:迈达斯软件正版多少钱 编辑:程序博客网 时间:2024/05/01 04:33

    题目URL:http://acm.hdu.edu.cn/showproblem.php?pid=1029;

    题目大意是找出数组中出现次数超过一半的数。基本思想是每遇到两个不同的数就消掉,设一个计数器就行了。存出现次数最大的那个数的出现次数。当下一个数与当前的数不同时,计数器减一,相同,则加一。

    贴个AC代码:

#include<iostream>#include<stdio.h>using namespace std;int main(){int cnt, max, cur, n;while(scanf("%d", &n) != EOF){cnt = 0;for(int i=0; i<n; i++){scanf("%d", &cur);if(!cnt){max = cur;cnt ++;}else if(cur != max) cnt --;else if(cur == max) cnt ++;}//cout << max << endl;printf("%d\n", max);}system("pause");return 0;}



原创粉丝点击