hdu 5166(水题)

Missing number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1020    Accepted Submission(s): 518


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. (T10)
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.(1n1,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
 
题意:现在知道一个序列中缺了两个数,问缺的这两个数的最小值是多少?
题解:标记已经有的数,扫一遍过去记录下最小的两个数而已.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
bool Hash[1005];
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        int n;
        scanf("%d",&n);
        memset(Hash,0,sizeof(Hash));
        int MIN = 1005,MAX = 0;
        for(int i=1;i<=n;i++){
            int v;
            scanf("%d",&v);
            Hash[v] = true;
            MIN = min(v,MIN);
            MAX = max(v,MAX);
        }
        int cnt = 0;
        int res[5];
        for(int v=1;;v++){
            if(!Hash[v]){
                res[cnt] = v;
                cnt++;
            }
            if(cnt==2) break;
        }
        printf("%d %d
",res[0],res[1]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5681353.html