hdu-1029 Ignatius and the Princess IV
生活随笔
收集整理的這篇文章主要介紹了
hdu-1029 Ignatius and the Princess IV
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:
http://acm.hdu.edu.cn/showproblem.php?pid=1029
題目類型:
給一個奇數(shù)個列的數(shù)組,其中一定存在某個數(shù)字,該數(shù)字的個數(shù)是大于一半的,問這個數(shù)字是幾
解題思路:
1、sort一遍,直接輸出下標(biāo)為n/2的數(shù)。
2、將數(shù)組開為1000010,然后將數(shù)組置0,讀入一個數(shù),下標(biāo)為該數(shù)的數(shù)值+1,最后進(jìn)行一次循環(huán),判斷那個數(shù)值最大,輸出即可。(當(dāng)年還不會快排所以用的這個方法,親測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
"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?
# include <stdio.h> # include <string.h> # define N 1000010 int 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; }?
轉(zhuǎn)載于:https://www.cnblogs.com/love-sherry/p/6941505.html
總結(jié)
以上是生活随笔為你收集整理的hdu-1029 Ignatius and the Princess IV的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。