hdu 1029 Ignatius and the Princess IV

本文介绍了一个算法问题,即从一组数中找出至少出现(N+1)/2次的特殊整数,并提供了三种解决方法:哈希计数、排序查找中位数及加减法。每种方法都附上了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ignatius and the Princess IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 13455    Accepted Submission(s): 5435


Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

"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 integer is the special one after I tell you all the integers." feng5166 says.

"But what is the characteristic of the special integer?" Ignatius asks.

"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 be my dinner, too. Hahahaha....." feng5166 says.

Can you find the special integer for Ignatius?
 

 

Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
 

 

Output
For each test case, you have to output only one line which contains the special number you have found.
 

 

Sample Input
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
 

 

Sample Output
3 5 1
 

 

Author
Ignatius.L
 

 

Recommend
We have carefully selected several similar problems for you:   1024  1260  1978  1421  1026
 
/*
    
    题意:
        给出n个数,保证有一个数最少出现(n+1)/2次,求该数
     类似于求众数
        
    有好几种做法:
    1、hash
    2、排序后的中位数    
    3、加减的不知道叫什么方法
     
*/
 
方法1:
 1 //250MS    4120K    371 B    G++
 2 /*
 3     这种方法要求出现的数要小于1000000 
 4 */
 5 #include<stdio.h>
 6 #include<string.h>
 7 int a[1000000];
 8 int main(void)
 9 {
10     int n,a0;
11     while(scanf("%d",&n)!=EOF)
12     {
13         memset(a,0,sizeof(a));
14         int maxn=0;
15         for(int i=0;i<n;i++){
16             scanf("%d",&a0);
17             a[a0]++;
18             if(a[a0]>a[maxn]) maxn=a0;
19         }
20         printf("%d\n",maxn);
21     }
22     return 0;
23 }
View Code

 

方法2:

 1 //296MS    1384K    363 B    G++
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 int a[1000000];
 5 int cmp(const void*a,const void*b)
 6 {
 7     return *(int*)a-*(int*)b;
 8 }  
 9 int main(void)
10 {
11     int n;
12     while(scanf("%d",&n)!=EOF)
13     {
14         for(int i=0;i<n;i++)
15             scanf("%d",&a[i]);
16         qsort(a,n,sizeof(a[0]),cmp);
17         printf("%d\n",a[(n+1)/2]);
18     }
19     return 0; 
20 }
View Code

 

方法3:

 1 //234MS    1380K    424 B    G++
 2 #include<stdio.h>
 3 int a[1000000];
 4 int main(void)
 5 {
 6     int n;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         for(int i=0;i<n;i++)
10             scanf("%d",&a[i]);
11         int maxn=0;
12         int count=0;
13         for(int i=0;i<n;i++){
14             if(count==0) maxn=a[i];
15             if(a[i]==maxn) count++;
16             else count--;
17         }    
18         printf("%d\n",maxn);
19         
20     }
21     return 0; 
22 }
View Code

 

 

转载于:https://www.cnblogs.com/GO-NO-1/articles/3384610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值