描述 Description
给你一个 n 个数的数列,其中某个数出现了超过 n div 2 次即众数,请你找出那个数。
输入格式 InputFormat
第 1 行一个正整数 n。
第 2 行 n 个正整数用空格隔开。
输出格式 OutputFormat
一行一个正整数表示那个众数。
样例输入 SampleInput
5
3 2 3 1 3
样例输出 SampleOutput
3
代码 Code
将每个数遇到相同的加一不相同减一最后剩下的就是众数。据说开 iostream 都会爆。。。
#include <stdio.h>
int n,m,tot,ans;
int main()
{
scanf("%d",&n);
while (n--)
{
scanf("%d",&m);
if (tot==0)
{
ans=m;
tot=0;
}
if (m==ans) tot++;else tot--;
}
printf("%d\n",ans);
return 0;
}