bupt-2014-1


思想借鉴王道的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++版博主葡萄家:

  1. #include<iostream>  
  2. #include<cstdio>  
  3. using namespace std;  
  4.   
  5. int a[105];  
  6.   
  7. int main()  
  8. {  
  9.     int tes;  
  10.     int n,i;  
  11.   
  12.     while(~scanf("%d",&tes))  
  13.     {  
  14.         while(tes--)  
  15.         {  
  16.             scanf("%d",&n);  
  17.             for(i=0; i<n; i++)  
  18.                 scanf("%d",&a[i]);  
  19.   
  20.             int cnt=1;   //记录出现的次数  
  21.             int res,ans=0;  
  22.             for(i=1; i<n; i++)  
  23.             {  
  24.                 if(a[i]!=a[i-1])  
  25.                 {  
  26.                     if(cnt>ans)  
  27.                     {  
  28.                         ans=cnt;  
  29.                         res=a[i-1];  
  30.                     }  
  31.                     cnt=1;  
  32.                 }  
  33.                 else  
  34.                     cnt++;  
  35.             }  
  36.             if(cnt>ans)   //最后一个数还没判断  
  37.             {  
  38.                 ans=cnt;  
  39.                 res=a[i-1];  
  40.             }  
  41.             printf("%d\n",res);  
  42.         }  
  43.     }  
  44.     return 0;  
  45. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值