zoukankan      html  css  js  c++  java
  • Bestcoder BestCoder Round #28 A 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<=10T10)
    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<=10001n1,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
     
    代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <queue>
     5 #define FOR(i, a, b) for(int i=a; i<b; i++)
     6 #define FOR_e(i, a, b) for(int i=a; i<=b; i++)
     7 
     8 using namespace std;
     9 int f[1001];
    10 int main()
    11 {
    12     int t, dd;
    13     int ma, mi;
    14     scanf("%d", &t);
    15     int i, n;
    16     while(t--)
    17     {
    18         scanf("%d", &n);
    19         memset(f, 0, sizeof(f));
    20         ma=-1; mi=2000;
    21         for(i=0; i<n; i++)
    22         {
    23             scanf("%d", &dd);
    24             f[dd]=1;
    25             if(dd>ma) ma=dd;
    26             if(dd<mi) mi=dd;
    27         }
    28         queue<int>q;
    29         while(!q.empty()) q.pop();
    30         for(i=mi; i<=ma; i++)
    31             if(f[i]==0)
    32             {
    33                 //printf("%d--", i);
    34                 q.push(i);
    35             }
    36         if(q.size()>=2)
    37         {
    38             dd=q.front(); q.pop(); printf("%d ", dd);
    39             dd=q.front(); q.pop(); printf("%d
    ", dd);
    40         }
    41         else if(q.size()==1)
    42         {
    43             dd=q.front(); q.pop(); printf("%d ", dd);
    44             if(mi==1)
    45             {
    46                 printf("%d
    ", ma+1);
    47             }
    48             else if(mi>1)
    49             {
    50                 printf("%d
    ", mi-1);
    51             }
    52         }
    53         else if(q.size()==0)
    54         {
    55             if(mi>2)
    56             {
    57                 printf("%d %d
    ", mi-2, mi-1);
    58             }
    59             else if(mi==2)
    60             {
    61                 printf("%d %d
    ", mi-1, ma+1);
    62             }
    63             else
    64             {
    65                 printf("%d %d
    ", ma+1, ma+2);
    66             }
    67         }
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    预习作业一
    20145234黄斐《java程序设计基础》第一周
    预习作业3
    开源 人脸识别 openface 实用介绍 实例演示 训练自己的模型
    ubuntu 开机 输入密码 无法进入
    linux anaconda 管理 python 包
    如何从零开始做一个蓝牙机械键盘
    keil中结构体跨文件调用
    GPRS 通信
    如何查找EI 及SCI 索引
  • 原文地址:https://www.cnblogs.com/yspworld/p/4279059.html
Copyright © 2011-2022 走看看