Bestcoder BestCoder Round #28 A Missing number(查找缺失的合法数字)
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. (T<=10T)
For each test case , the first line contains a integers nn , which means the number of numbers the permutation has. In following a line ,
For each test case , the first line contains a integers nn , which means the number of numbers the permutation has. In following a line ,
there are nnN distinct postive integers.(1<=n<=10001≤n≤1,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
Source
代码:
View Code
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <queue> 5 #define FOR(i, a, b) for(int i=a; i<b; i++) 6 #define FOR_e(i, a, b) for(int i=a; i<=b; i++) 7 8 using namespace std; 9 int f[1001]; 10 int main() 11 { 12 int t, dd; 13 int ma, mi; 14 scanf("%d", &t); 15 int i, n; 16 while(t--) 17 { 18 scanf("%d", &n); 19 memset(f, 0, sizeof(f)); 20 ma=-1; mi=2000; 21 for(i=0; i<n; i++) 22 { 23 scanf("%d", &dd); 24 f[dd]=1; 25 if(dd>ma) ma=dd; 26 if(dd<mi) mi=dd; 27 } 28 queue<int>q; 29 while(!q.empty()) q.pop(); 30 for(i=mi; i<=ma; i++) 31 if(f[i]==0) 32 { 33 //printf("%d--", i); 34 q.push(i); 35 } 36 if(q.size()>=2) 37 { 38 dd=q.front(); q.pop(); printf("%d ", dd); 39 dd=q.front(); q.pop(); printf("%d\n", dd); 40 } 41 else if(q.size()==1) 42 { 43 dd=q.front(); q.pop(); printf("%d ", dd); 44 if(mi==1) 45 { 46 printf("%d\n", ma+1); 47 } 48 else if(mi>1) 49 { 50 printf("%d\n", mi-1); 51 } 52 } 53 else if(q.size()==0) 54 { 55 if(mi>2) 56 { 57 printf("%d %d\n", mi-2, mi-1); 58 } 59 else if(mi==2) 60 { 61 printf("%d %d\n", mi-1, ma+1); 62 } 63 else 64 { 65 printf("%d %d\n", ma+1, ma+2); 66 } 67 } 68 } 69 return 0; 70 }