这道题非常水,主要目的是想把网上看到的一种解法收藏下……
思路:本题方法很多:map,哈希,排序(中位数)……网上看到一种利用鸽巢原理的,代码如下:
转自:http://blog.csdn.net/welcome_z/article/details/7855032
#include <cstdio> #include <cstdlib> int main () { int ans,num,count,n; while (~scanf("%d", &n)) { count = 0; for (int i=1; i<=n; i++) { scanf("%d", &ans); if (count == 0) { num = ans; count ++; } else if (ans == num) count++; else count--; } printf("%d\n", num); } return 0; }