您的位置:首页 > 其它

HDU 1029 - Ignatius and the Princess IV

2017-09-06 15:22 441 查看
题目大意:输入一个奇数N,接着输入N个数,找出其中出现个数为(N+1)/2的数。

解题思路:因为出现次数为奇数+1的一半,也就是出现次数大于N/2的数。用map存下出现的数以及出现的次数。

ac代码:
#include <iostream>
#include <map>
using namespace std;
map <int, int>ma;
int n, t, re;
int main()
{
while (scanf("%d", &n) != EOF){
for (int i=0; i<n; i++){
scanf("%d", &t);
ma[t]++;
if (ma[t] > n/2)
re = t;
}
printf("%d\n", re);
ma.clear();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: