Http://www.cnblogs.com/joeylee97/p/6616039.html
With the introduction of a CNT, the INPUT element is the same as the previous element, CNT increases, otherwise CNT decreases, when CNT is zero, the input element is recorded, because the desired number appears at least (n+1)/2 times, so the last recording element is the element that is being asked for
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <cmath>6#include <algorithm>7 8 using namespacestd;9 Const intmaxn=1e6+Ten;Ten One intN; A intMain () - { - while(SCANF ("%d", &n)! =EOF) the { - intTemp,cur,cnt=1; -scanf"%d",&temp); - for(intI=1; i<n;i++) + { -scanf"%d",&cur); + if(cur==temp) A { atcnt++; - } - Else - { -cnt--; - } in if(cnt==0) - { totemp=cur; +Cnt=1; - } the * } $printf"%d\n", temp);Panax Notoginseng } - return 0; the}
View Code
"Algorithm Series learning" [Kuangbin you fly] topic 12 Basic DP1 B-ignatius and the Princess IV