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

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

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

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
LL x; int n;
int main ()
{ while (~scanf ("%d",&n) ) {
int sum=; LL ans;
for (int i=;i<=n;i++) {
scanf ("%lld",&x);
if (sum==) {
ans=x;
sum++; // 易错 我是太蠢了嘛。。。
}
else {
if (x==ans) sum++;
else sum--;
}
}
printf ("%lld\n",ans);
}
return ;
}