Description
Give you a number of n series, in which a number of more than n / 2 times , you should find out that the number (n<=500000,x<=10^19)
Input
The first line is a positive integer n. The second line of n positive integers separated by spaces.
Output
A Positive integer on a line indicates that number
Sample Input
5
3 2 3 1 3
Sample Output
3
思路
输出序列中出现次数超过n/2次的数
代码
#include<iostream>
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
string str,ans="0";
map<string,int> s;
for(int i=0;i<n;i++)
{
cin>>str;
s[str]+=1;
if(s[str]>n/2) ans=str;
}
if(ans!="0")
cout<<ans<<endl;
}
return 0;
}
本文介绍了一种算法,用于从一系列正整数中找出出现次数超过总数一半的数字。通过使用C++实现,包括输入处理、查找主导数以及输出结果的过程。示例输入输出帮助理解算法效果。
1076

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



