zoukankan      html  css  js  c++  java
  • Missing number

    Missing number

    题目:

    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
    题意:
    有一个长度为n+2的排列少了两数,找出这两个数,要求这两个数是最小的。
    分析:
    定义一个数组a,并且将它清零,如果数组中有数字x就将1赋值a[x],
    后从i=1开始查找如果a[i]为零就将i输出。
    说明:注意输出的格式和输出的个数。
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 const int maxn=1000;
     5 int a[maxn];
     6 int main()
     7 {
     8 int T,n,x,j;
     9 int b[5];
    10 cin>>T;
    11 while(T--)
    12 {
    13     memset(a,0,sizeof(a));
    14  cin>>n;
    15 for(int i=0;i<n;i++)
    16 {cin>>x;
    17 a[x]=1;
    18 }
    19 j=0;
    20 for(int k=1;k<=n+2;k++)
    21 if(!a[k])
    22 b[j++]=k;
    23 cout<<b[0]<<' '<<b[1]<<endl;
    24 }
    25 return 0;
    26 }
     
     
     
  • 相关阅读:
    log4js 2.X版本配置详解
    ping -c 3 localhost
    children_process.exec 执行 ping命令报错
    淘宝双十一为什么会出现通道拥挤?
    'ascii' codec can't decode byte 0xd6 in position 0
    window 安装grunt
    display:none与visible:hidden的区别 slideDown与
    ztree-demo 2
    ztree-demo
    技术总结PHP+微信
  • 原文地址:https://www.cnblogs.com/fenhong/p/4656234.html
Copyright © 2011-2022 走看看