hdoj1029 Ignatius and the Princess IV(对题目的观察?)

来源:互联网 发布:锐捷客户端for mac 编辑:程序博客网 时间:2024/06/05 23:02

这道题以前在408统考的卷子上做过。

首先这道题数据量大,容易报超时。其次我们通过理解题意,知道是要找到出现次数超过一半的数,而且关键在于输入的是奇数个数

这意味着出现次数最多的那个数起码会连着出现两次。

我们用count来筛选,初始值为0,结果变量设为temp,输出当count不为0时的temp值。

注:此题用cin输入也会超时。

#include<stdio.h>int main(){int n,count,a,i,temp;while(scanf("%d",&n)!=EOF){count=0;for(i=0;i<n;i++){scanf("%d",&a);if(count==0){temp=a;count++;}else{if(temp==a)count++;else count--;}}printf("%d\n",temp);}return 0;}