Missing number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1018 Accepted Submission(s): 517
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
思路:初始序列不是排好序的。。注意。 然后直接模拟就好了,输入保证插入两个数一定可以组成
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1050
int a[N];
int ans[3];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
int num=0;
if(a[n]-a[1]+1==n)
{
if(a[1]>2) printf("%d %d\n",a[1]-2,a[1]-1);
else if(a[1]==2) printf("1 %d\n",a[n]+1);
else printf("%d %d\n",a[n]+1,a[n]+2);
}
else
{
for(int i=1; i<n; i++)
{
if(a[i]+2==a[i+1])
ans[num++]=a[i]+1;
else if(a[i]+3==a[i+1])
{
ans[num++]=a[i]+1;
ans[num++]=a[i]+2;
}
if(num>=2)
break;
}
if(num==1)
{
if(a[1]==1)
printf("%d %d\n",ans[0],a[n]+1);
else
printf("1 %d\n",ans[0]);
}
else printf("%d %d\n",ans[0],ans[1]);
}
}
return 0;
}