Missing number

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 }

 

 
 
 
posted @ 2015-07-18 09:33  枫虹  阅读(118)  评论(0编辑  收藏  举报