码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-01 23:23:46      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

  • 时间: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. }




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

标签:

原文地址:http://www.cnblogs.com/qhy285571052/p/de830226064940694c0b509c320f9c6c.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!