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
代码:
View Code
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
相关文章推荐
- BestCoder Round #28
- BestCoder Round #28
- 【BestCoder Round 65C】【树状数组 动态查找第k大 O(nlogn)】ZYB's Premutation 告诉你前i个数中的逆序对数让你还原全排列
- BestCoder Round #33——1001——zhx's submissions
- BestCoder Round #43
- bestcoder round 71 div2
- 【HDU5188 BestCoder Round 33C】【贪心排序+DP】zhx and contest 考试不被怀疑作弊条件下达到至少m分的最少时间
- KK's Steel bestcoder round 71 hdu 5620(裴波那契)
- hdu 4859 海岸线 Bestcoder Round 1
- BestCoder Round #75 King's Cake 模拟&&优化 || gcd
- 【BestCoder】HDOJ 5062 Beautiful Palindrome Number
- hdu 5154 Harry and Magical Computer(BestCoder Round #25)
- 【BestCoder Round 65D】【树形DP 容斥思想】ZYB's Tree 求距离每个节点距离不超过k的节点数
- BestCoder Round 38-1002 Greatest Greatest Common Divisor
- 【HDU5747 BestCoder Round 84A】【贪心 细节】Aaronson m范围最少数量2的幂凑成n
- HDU5105 Math Problem && BestCoder Round #18 1002
- BestCoder Round #50 The mook jong 即hdu5366 (动态规划)
- BestCoder Round #18 1004 &&hdu 5107 K-short Problem 线段树
- BestCoder Round #20
- BestCoder Round #59 HDOJ5499 5500 5501题解