#include
<
stdio.h
>
#include
<
stdlib.h
>
int
main()
...
{ long n; long m; while(scanf("%ld", &n)!= EOF) ...{ scanf("%ld",&m); long temp = m; long count = 1; int i; for(i = 1;i < n; i ++) ...{ scanf("%d", &m); if(!count ) ...{ temp = m; count = 1; } else if(temp == m) ...{ count ++; } else if(temp!=m) count--; } printf("%d ",temp); } system("pause"); return 0; }
一次ac了,完美的zoj 第100题,
说下算法
Sample Input
5 2 1 2 3 2
8 3 3 4 4 4 4 3 4
Sample Output
2
4
因为最多的数超过一半,例如第一个sample中,2是最多的,你可以将每个2都分配给不同的 其它元素,最后2还是有剩余
就像是同归于尽一样,最后剩下的就是其中最多的元素了!
利用这个我们可以定义一个当前数和当前数的个数,与下一个比,相同则加一,不同则减一知道个数为0时,
当前数变成下一个,知道输入结束!存活下来的就是
The Most Frequent Number了