[2016-03-27][HDU][1029][Ignatius and the Princess IV]

  • 时间:2016-03-30 22:03:01 星期三

  • 题目编号:[2016-03-27][HDU][1029][Ignatius and the Princess IV]

  • 题目大意:给定n个数字,输出其中出现次数超过n/2的数字

  1. #include <algorithm>
  2. #include <cstdio>
  3. using namespace std;
  4. const int maxn = 1E6 + 10;
  5. int a[maxn];
  6. int main(){
  7. int n;
  8. while(~scanf("%d",&n)){
  9. for(int i = 1;i <= n ;++i){
  10. scanf("%d",&a[i]);
  11. }
  12. sort(a,a+n);
  13. a[0] = a[1] - 1;
  14. int i,cnt = 0;
  15. for(i = 1;i <= n ;++i){
  16. if(a[i] == a[i - 1]) ++cnt;
  17. else cnt = 1;
  18. if(cnt >= (n + 1)/2) break;
  19. }
  20. printf("%d\n",a[i]);
  21. }
  22. return 0;
  23. }


来自为知笔记(Wiz)


posted on 2016-04-01 21:47  红洋  阅读(134)  评论(0)    收藏  举报

导航