1029 hdu

  1. *Ignatius and the Princess IV 
  2. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others) 
  3. Total Submission(s): 21309    Accepted Submission(s): 8838 
  4.  
  5.  
  6. Problem Description 
  7. "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. 
  8.  
  9. "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  
  10. integer is the special one after I tell you all the integers." feng5166 says. 
  11.  
  12. "But what is the characteristic of the special integer?" Ignatius asks. 
  13.  
  14. "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  
  15. be my dinner, too. Hahahaha....." feng5166 says. 
  16.  
  17. Can you find the special integer for Ignatius? 
  18.  
  19. Input 
  20. The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer  
  21. N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers.  
  22. The input is terminated by the end of file. 
  23.  
  24. Output 
  25. For each test case, you have to output only one line which contains the special number you have found. 
  26.  
  27. Sample Input 
  28. 5 
  29. 1 3 2 3 3 
  30. 11 
  31. 1 1 1 1 1 5 5 5 5 5 5 
  32. 7 
  33. 1 1 1 1 1 1 1 
  34.  
  35. Sample Output 
  36. 3 
  37. 5 
  38. 1 

题是我搬别人的大笑

代码是自己写了一晚弄出来的,小白一个委屈

#include<stdio.h>

#include<memory.h>//包含memset函数 
const int MAX=1000000;
int a[MAX];
int main()
{
int n;

while(~scanf("%d",&n))
{   memset(a,0,sizeof(a));//初始化数组 
int m=0,j=0;
for(int i=0;i<n;i++)
{
scanf("%d",&j);
if(j>m)
m=j;//为了方便下面检索需要测到数组中a[某] ,节省时间 
a[j]++;//a[j]中存的是j出现的次数 
}
for(j=1;j<=m;j++)
{
if(a[j]>=(n+1)/2)
{printf("%d\n",j);break;}//既然已经有一个了,且一定仅有一个,直接跳出 
}
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值