hdu--1029 编程之美 在数组a中 (元素个数n n是奇数)找一个数字 它出现的次数大于(n+1)/2

我为什么总是犯这些愚蠢错误啊,还是自己逻辑不够严谨。 努力ing......

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 typedef long long LL; 
 7 LL x; int n;
 8 int main ()
 9 {
10 
11     while (~scanf ("%d",&n) ) {
12         int sum=0; LL ans;
13         for (int i=1;i<=n;i++) {
14             scanf ("%lld",&x);
15             if (sum==0) {
16                 ans=x;
17                 sum++;  // 易错  我是太蠢了嘛。。。
18             }
19             else {
20                 if (x==ans) sum++;
21                 else        sum--;
22             }
23         }
24         printf ("%lld
",ans);
25     }
26     return 0;
27 }
抓住青春的尾巴。。。
原文地址:https://www.cnblogs.com/xidian-mao/p/9409379.html