首页 > 代码库 > zoj2132-The Most Frequent Number

zoj2132-The Most Frequent Number

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2132

题意很简单。这里采用O(1)算法。

 1 #include <cstdio> 2  3 int main() 4 { 5     int n,ans; 6     while(~scanf("%d",&n)) 7     { 8         int m,cnt=0; 9         for(int i=0;i<n;i++)10         {11             scanf("%d",&m);12             if(cnt==0)  ans=m,cnt++;13             else if(ans==m) cnt++;14             else    cnt--;15         }16         printf("%d\n",ans);17     }18     return 0;19 }
View Code

 

zoj2132-The Most Frequent Number