摘要:找出给定数据中出现次数最多的数
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
3
5
1
题目理解:对给出的数进行计数 并时刻关注当前出现次数最多的数。使用map来帮忙储存。
注意:给出的数据范围并没有确定,刚开始没有get到这个点。WA了好久
2017 05 19
代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <memory.h>#include <map>using namespace std;map<int,int> N;int main(){ int n,t,ans=0; while(~scanf("%d",&n)){ N.clear();ans=0;N[0]=0; for(int i=0;i<n;i++){ scanf("%d",&t); N[t]++; if(N[ans]<N[t]) ans=t; } printf("%d\n",ans); }}
本文介绍了一个算法问题,即从一组给定的整数中找出出现次数超过(N+1)/2次的特殊整数。该文提供了完整的代码实现,并强调了处理未明确数据范围的重要性。
324

被折叠的 条评论
为什么被折叠?



