您的位置:首页 > 其它

Bestcoder BestCoder Round #28 A Missing number(查找缺失的合法数字)

2015-02-07 17:10 447 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
There is a number T shows there are T test cases below. (T<=10T≤10)
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)

[align=left]Output[/align]
For each case output two numbers , small number first.

[align=left]Sample Input[/align]

2

3

3 4 5
1

1

[align=left]Sample Output[/align]

1 2

2 3

[align=left]Source[/align]
BestCoder Round #28

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define FOR_e(i, a, b) for(int i=a; i<=b; i++)

using namespace std;
int f[1001];
int main()
{
int t, dd;
int ma, mi;
scanf("%d", &t);
int i, n;
while(t--)
{
scanf("%d", &n);
memset(f, 0, sizeof(f));
ma=-1; mi=2000;
for(i=0; i<n; i++)
{
scanf("%d", &dd);
f[dd]=1;
if(dd>ma) ma=dd;
if(dd<mi) mi=dd;
}
queue<int>q;
while(!q.empty()) q.pop();
for(i=mi; i<=ma; i++)
if(f[i]==0)
{
//printf("%d--", i);
q.push(i);
}
if(q.size()>=2)
{
dd=q.front(); q.pop(); printf("%d ", dd);
dd=q.front(); q.pop(); printf("%d\n", dd);
}
else if(q.size()==1)
{
dd=q.front(); q.pop(); printf("%d ", dd);
if(mi==1)
{
printf("%d\n", ma+1);
}
else if(mi>1)
{
printf("%d\n", mi-1);
}
}
else if(q.size()==0)
{
if(mi>2)
{
printf("%d %d\n", mi-2, mi-1);
}
else if(mi==2)
{
printf("%d %d\n", mi-1, ma+1);
}
else
{
printf("%d %d\n", ma+1, ma+2);
}
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: