hdu1029

对于水题, 想到就超级简单,没有头绪就完了。。。

所以说数学很重要,思维想法很重要!!!

这道题题意就是:给你一个奇数n, 然后让你输出n个数字中出现次数超过(n+1)/2 的那个数

其实很简单,因为题目中没有说如果没有这样的数存在该如何操作,也就是说给你的一组数据一定会有一个数字出现次数大于等于(n+1)/2

因此事情就变得很简单,只要将这组数据排序,输出中间那个数就行了,因为给你的n是奇数不会存在二选一的情况!!

 1 #include <iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int N = 1000000;
 7 int a[N];
 8 
 9 int main()
10 {
11     int n;
12     while(scanf("%d",&n)!=EOF)
13     {
14         for (int i = 0;i<n;i++)
15           scanf("%d",&a[i]);
16         sort(a, a+n);
17         printf("%d
",a[(n+1)/2]);
18         memset(mark,0,sizeof(a));
19     }
20     return 0;
21 }
View Code

这里有一点要说明:在输入数组数据时最好使用scanf, 用cin输入的话相比于scanf 是很耗时间的,会超时!!!

Just do what you want!
原文地址:https://www.cnblogs.com/shirley-0021/p/8511036.html