Missing number

本文介绍了一种通过初始化数组并标记已知数的方法来找出给定排列中缺失的两个数的算法。该方法适用于长度为n+2的排列,且其中有两数缺失的情况。通过对输入的整数进行处理,可以有效地找出最小的两个缺失数。

Missing number

题目:

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
题意:
有一个长度为n+2的排列少了两数,找出这两个数,要求这两个数是最小的。
分析:
定义一个数组a,并且将它清零,如果数组中有数字x就将1赋值a[x],
后从i=1开始查找如果a[i]为零就将i输出。
说明:注意输出的格式和输出的个数。
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=1000;
 5 int a[maxn];
 6 int main()
 7 {
 8 int T,n,x,j;
 9 int b[5];
10 cin>>T;
11 while(T--)
12 {
13     memset(a,0,sizeof(a));
14  cin>>n;
15 for(int i=0;i<n;i++)
16 {cin>>x;
17 a[x]=1;
18 }
19 j=0;
20 for(int k=1;k<=n+2;k++)
21 if(!a[k])
22 b[j++]=k;
23 cout<<b[0]<<' '<<b[1]<<endl;
24 }
25 return 0;
26 }

 

 
 
 

转载于:https://www.cnblogs.com/fenhong/p/4656234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值