Ignatius and the Princess IV
这道题给出的测试样例都一定存在一个special integer。
我们可以利用数组进行存储及遍历,也可以用map进行存储。
#include<iostream>
#include<map>
using namespace std;
map<int,int>ma;
int main(){
int n;
while(cin>>n&&n){
ma.clear();//多组输入一定要记得初始化
for(int i=0;i<n;i++){
int s;
cin>>s;
ma[s]++;//将键(key)s对应的值(value)+1
}
int maxx=0;
int k;
map<int,int>::iterator it;//迭代器
for(it=ma.begin();it!=ma.end();it++){//左闭右开区间
int l=it->second;
if(l>maxx){//遍历找到最大值
k=it->first;
maxx=l;
}
}
if(maxx>=(n+1)/2)
cout<<k<<endl;//输出这个特殊的数
}
return 0;
}