Ignatius and the Princess IV(暴力)
2017-08-17 13:58
344 查看
"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
Sample Output
题意:
在N个数字中,找到出现次数大于(N+1)/2的数字,N为奇数
思路:
由于题目保证这个数字一定出现,所以每个数字出现的次数记录下来,找到最大的
代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>.
using namespace std;
int book[1000000];//记录数字在num数组的位置,如果没出现则为-1
struct node{
int num;//存数字
int ans;//存该数字的出现次数
}dp[1000000];
int cmp(node a,node b){
return a.ans>b.ans;
}
int n;
int main(){
while(scanf("%d",&n)!=EOF){
memset(book,-1,sizeof(book));
memset(dp,0,sizeof(dp));
int m=(n+1)/2;
int cnt=0;
int i;
for(i=0;i<n;i++){
int a;
scanf("%d",&a);
if(book[a]==-1){//没出现过
book[a]=cnt;
dp[cnt].num=a;
dp[cnt++].ans++;
}
else{//出现过,找到在num数组的位置
int b=book[a];
dp[b].ans++;
}
}
sort(dp,dp+cnt,cmp);
for(i=0;i<cnt;i++)
printf("%d\n",dp[0].num);
}
return 0;
}
"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
题意:
在N个数字中,找到出现次数大于(N+1)/2的数字,N为奇数
思路:
由于题目保证这个数字一定出现,所以每个数字出现的次数记录下来,找到最大的
代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>.
using namespace std;
int book[1000000];//记录数字在num数组的位置,如果没出现则为-1
struct node{
int num;//存数字
int ans;//存该数字的出现次数
}dp[1000000];
int cmp(node a,node b){
return a.ans>b.ans;
}
int n;
int main(){
while(scanf("%d",&n)!=EOF){
memset(book,-1,sizeof(book));
memset(dp,0,sizeof(dp));
int m=(n+1)/2;
int cnt=0;
int i;
for(i=0;i<n;i++){
int a;
scanf("%d",&a);
if(book[a]==-1){//没出现过
book[a]=cnt;
dp[cnt].num=a;
dp[cnt++].ans++;
}
else{//出现过,找到在num数组的位置
int b=book[a];
dp[b].ans++;
}
}
sort(dp,dp+cnt,cmp);
for(i=0;i<cnt;i++)
printf("%d\n",dp[0].num);
}
return 0;
}
相关文章推荐
- HDU - 1029 Ignatius and the Princess IV(暴力)
- B - Ignatius and the Princess IV HDU - 1029 暴力求解
- Ignatius and the Princess IV HDU - 1029
- HDU—— 1029 Ignatius and the Princess IV
- HDU1029——Ignatius and the Princess IV
- Ignatius and the Princess IV
- hpu暑假训练 B - Ignatius and the Princess IV 【sort】
- HDU 1029 Ignatius and the Princess IV
- hdu-1029 Ignatius and the Princess IV
- Ignatius and the Princess IV
- Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV【】
- Ignatius and the Princess IV(乱搞一发竟然过了)
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV(水题)
- B - Ignatius and the Princess IV HDU-1029
- hdoj1029 Ignatius and the Princess IV(对题目的观察?)
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV DP
- HDU 1029 Ignatius and the Princess IV --- 水题