码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1029 Ignatius and the Princess IV

时间:2015-06-20 17:11:57      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:dp   c   

题意:找出出现次数大于(N+1)/2的数。

思路:开一个一维数组dp[ 100 0000 ] --- 数组的序号与数出现的次数匹配

 例如:

1 2 1 1 7

dp[1] = 3;

dp[2]= 1;

dp[7] = 1;

#include<stdio.h>

int dp[1000000];//100万,初始化为0

int main(){
    int n=0,temp = 0,i,yes;
    while(scanf("%d",&n)!=EOF){
        //初始化
        for(i=0;i<1000000;i++){
            dp[i]=0;
        }
        for(i=0;i<n;i++){
            scanf("%d",&temp);
            dp[temp]++;
            if(dp[temp]>=(n+1)/2){
                yes=temp;//标记
            }
        }
        printf("%d\n",yes);
    }
    return 0;
}








Ignatius and the Princess IV

伊格内修斯和四公主
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 20766    Accepted Submission(s): 8574


Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
”好吧,你不是太糟糕了,他们……但你永远不能通过接下来的考试。“ feng5166说。
"I will tell you an odd number N, and then N integers. There will be a special integer among them, 
“ 我将告诉你一个奇数N,然后N个整数。在N个数之间有一个特殊的整数,
you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
你的目标是找出这个整数的位置。”  feng5166说。
"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.
”这个整数至少出现 (N+1)/2次。如果你找不到这个整数,我就杀死公主,你也会成为我的晚餐。哈哈。。。“  feng5166说。
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.
接着 第二行有N个数。当输入数据到达文件末尾是,表示输入结束。

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

hdu 1029 Ignatius and the Princess IV

标签:dp   c   

原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46573613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!