首页 > 代码库 > hdu-1029 Ignatius and the Princess IV
hdu-1029 Ignatius and the Princess IV
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1029
题目类型:
给一个奇数个列的数组,其中一定存在某个数字,该数字的个数是大于一半的,问这个数字是几
解题思路:
1、sort一遍,直接输出下标为n/2的数。
2、将数组开为1000010,然后将数组置0,读入一个数,下标为该数的数值+1,最后进行一次循环,判断那个数值最大,输出即可。(当年还不会快排所以用的这个方法,亲测AC)。
题目:
Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 31704 Accepted Submission(s): 13646
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?
"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
51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1
Sample Output
351
# include <stdio.h># include <string.h># define N 1000010int n,a[N],b[N];int main (){ int i,ret,max; while(scanf("%d",&n)!=EOF) { memset(b,0,sizeof(b)); max=0; ret=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); b[a[i]]++; if(b[a[i]] >max) { max=b[a[i]]; ret=a[i]; } } printf("%d\n",ret); } return 0;}
hdu-1029 Ignatius and the Princess IV
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。