思想借鉴王道的hash[],由于题目中并未提到整数是否正数,所以考虑小于0的情况,用偏移量offset将”负数转为正数”,考虑得有些复杂,直接考虑了整数次数出现的情况,而题目是给出的非降的数列可以在简化一些,从第一个所给的整数开始比较简单吧
C语言:
#include<stdio.h>
#include<stdlib.h>#define offset 100000
int main(){
int i,N,d,max,temp,num;
int hash[2*offset]={0};
while(scanf("%d",&num)!=EOF){
while(num--){
temp=0;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d",&d);
hash[d+offset]++;
}
max=hash[0];
for(i=0;i<2*offset;i++){
if(hash[i]>max){
max=hash[i];
temp=i;
}
}
printf("%d\n",temp-offset);
}
}
system("pause");
return 0;
}
c++版:
#include<iostream>
using namespace std;
#define offset 100000
int main(){
int i,N,d,max,temp,num;
int hash[2*offset]={0};
while(cin>>num){
while(num--){
temp=0;
cin>>N;
for(i=0;i<N;i++){
cin>>d;
hash[d+offset]++;
}
max=hash[0];
for(i=0;i<2*offset;i++){
if(hash[i]>max){
max=hash[i];
temp=i;
}
}
cout<<temp-offset<<endl;
}
}
return 0;
}
c++版博主葡萄家:
- #include<iostream>
- #include<cstdio>
- using namespace std;
- int a[105];
- int main()
- {
- int tes;
- int n,i;
- while(~scanf("%d",&tes))
- {
- while(tes--)
- {
- scanf("%d",&n);
- for(i=0; i<n; i++)
- scanf("%d",&a[i]);
- int cnt=1; //记录出现的次数
- int res,ans=0;
- for(i=1; i<n; i++)
- {
- if(a[i]!=a[i-1])
- {
- if(cnt>ans)
- {
- ans=cnt;
- res=a[i-1];
- }
- cnt=1;
- }
- else
- cnt++;
- }
- if(cnt>ans) //最后一个数还没判断
- {
- ans=cnt;
- res=a[i-1];
- }
- printf("%d\n",res);
- }
- }
- return 0;
- }