hdu1029 B - Ignatius and the Princess IV
2015-12-04 21:24
239 查看
B - Ignatius and the Princess IV
Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
1029
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int MAXN =1000000;
int d[MAXN];
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
int main()
{
int N,Max,num;
while(cin>>N)
{
Max=-9;
memset(d,0,sizeof(d));
for(int i=0;i<N;i++)
{
scanf("%d",&num);
Max=max(num,Max);
d[num]++;
}
int k;
for(k=1;k<=Max;k++)
{
if(d[k]>=((N+1)/2))
{
cout<<k;
break;
}
}
for(int i=k+1;i<=Max;i++)
{
if(d[i]>=(N/2+1))
cout<<" "<<i;
}
cout<<endl;
}
return 0;
}
Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
1029
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int MAXN =1000000;
int d[MAXN];
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
int main()
{
int N,Max,num;
while(cin>>N)
{
Max=-9;
memset(d,0,sizeof(d));
for(int i=0;i<N;i++)
{
scanf("%d",&num);
Max=max(num,Max);
d[num]++;
}
int k;
for(k=1;k<=Max;k++)
{
if(d[k]>=((N+1)/2))
{
cout<<k;
break;
}
}
for(int i=k+1;i<=Max;i++)
{
if(d[i]>=(N/2+1))
cout<<" "<<i;
}
cout<<endl;
}
return 0;
}
相关文章推荐
- light--oj--1116--Ekka Dokka(数学问题)
- python基础教程共60课-第3课IDE
- project euler 41
- 初探动态规划之数字三角形
- linux 安装mysql
- 利用JQuery jqprint实现打印功能
- 计算理论中的莱斯定理(Rice's Theorem)——证明与应用
- [置顶] 亮仔移植u-boot系列之-- S3c2440在最新版本U-boot-2015.10移植(支持SPL模式启动) -- 1
- Android Studio 快捷键
- Android自定义组件之圆形图片
- OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(1)
- 用eclipse创建servlet小程序
- POJ 2352 Stars (区间建树,单点更新)
- cf C - Booking System
- centos下httpd-2.4的编译安装
- leetcode Count and Say
- project euler 40
- centos下httpd-2.4的编译安装
- android 布局优化(一)
- 问题处理:找不到Pch预编译文件?