HDU - 5166 Missing number

Missing number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1145    Accepted Submission(s): 590


Problem Description
There is a permutation without two numbers in it, and now you know what numbers the permutation has. Please find the two numbers it lose.
 

Input
There is a number T shows there are T test cases below. ( T10 )
For each test case , the first line contains a integers n , which means the number of numbers the permutation has. In following a line , there are n distinct postive integers.( 1n1,000 )
 

Output
For each case output two numbers , small number first.
 

Sample Input
  
  
2 3 3 4 5 1 1
 

Sample Output
  
  
1 2 2 3
题意:在一组数中缺少了两个,把他们找出来,小数优先输出。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

int main(){
	int t;
	int s[1001];
	scanf("%d",&t);
	while(t--){
		memset(s,0,sizeof(s));
		int n,q,maxn=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&q);
			s[q]=1;
			maxn=max(maxn,q);
		}
		int i;
		for(i=1;i<=maxn;i++){
			if(s[i]==1){
				break;
			}
		}
		int sum=0;
		for(int j=i;j<=maxn;j++){
			if(s[j]!=1){
				sum++;
			}
		}
		int flag=1;
		if(sum==2){			
			for(int j=i;j<=maxn;j++){
				if(s[j]!=1){
					if(flag==1){
						flag=0;
					}
					else{
						printf(" ");
					}
					printf("%d",j);
				}
			}
			printf("\n");
		}
		if(sum==1&&i==1){
			for(int j=i;j<=maxn;j++){
				if(s[j]!=1){
					printf("%d ",j);
					break;
				}
			}
			printf("%d\n",maxn+1);
		}
		if(sum==1&&i>1){
			printf("%d ",i-1);
			for(int j=i;j<=maxn;j++){
				if(s[j]!=1){
					printf("%d\n",j);
					break;
				}
			}
		}
		if(sum==0&&i==2){
			printf("1 %d\n",maxn+1);
		}
		if(sum==0&&i==1){
			printf("%d %d\n",maxn+1,maxn+2);
		}
		if(sum==0&&i>2){
			printf("%d %d\n",i-2,i-1);
		}
		//printf("%d %d\n",sum,i);
	}
	return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值