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. (
T≤10
)
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.( 1≤n≤1,000 )
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.( 1≤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
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;
}