HDU 5166 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≤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)
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<cstring>
#include<cstdio>
using namespace std;
const int Max=1010;
int num[Max];
int main()
{
int T;
int num_result[2];
scanf("%d",&T);
while(T--)
{
memset(num,0,sizeof(num)); //用0初始化数组num
int n,test;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&test);
num[test]=true; //下标标记输入的数字
}
int j=0;
for(int i=1;i<=n+2;i++)
{
if(num[i]==0)
num_result[j++]=i;
}
printf("%d %d",num_result[0],num_result[1]);
cout<<"\n";
}
return 0;
}
memset函数作用
memset(num,0,sizeof(num)) 使数组num都初始化为0;