首页 > 其他 > 详细

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

时间:2015-07-17 17:46:39      阅读:131      评论:0      收藏:0      [点我收藏+]
 

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;

}

 

 

 

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

原文:http://www.cnblogs.com/xinxiangqing/p/4654667.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!