• 输入一串数字找出其中缺少的最小的两个数


     

    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 Tshows 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<=1000)

     

    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 <string.h>

    using namespace std;

    const int maxn=10005;

    int a[maxn];

    int main()

    {

            int T;

            cin>>T;

            while(T--)

            {

                memset(a,0,sizeof(a));//将数组清零

                    int n,t;

                    cin>>n;

                    for(int i=0;i<n;i++)

                    {

                             cin>>t;

                   a[t]++;// 将输入的数作为这个数组的一个下标,同时将数组中的数

    //++,使其不再为零即可

                    }

                    int c=0;

                    for(int k=1;c<2;k++)

                    {

                             if(a[k]==0)//判断数组中的数是否为零

                             {

                                      c++;

                                 cout<<k;

                                      if(c==1)//控制空格的输出

                                              cout<<" ";

                             }

           

                    }

                    cout<<endl;

            }

     

            return 0;

    }

     

     
  • 相关阅读:
    【牛客】找出单向链表中的一个节点,该节点到尾指针的距离为K
    【牛客】牛妹的礼物 (动态规划)
    【问题解决】Anaconda连不上服务器,"服务器正在启动,请稍等",报错信息:ImportError: cannot import name 'create_prompt_application'
    将一个数组的各元素插入到链表中,并以升序排列
    链式队列的相关操作
    循环队列的基本操作
    栈的相关操作
    双链表的基本操作
    单链表的相关操作
    顺序表的相关操作
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4654667.html
Copyright © 2020-2023  润新知