排列(c++ stl+哈希)

来源:互联网 发布:淘宝优惠券那个网站好 编辑:程序博客网 时间:2024/06/02 02:27

Problem E: 排列

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 64  Solved: 16
[Submit][Status][Web Board]

Description

Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。

Input

第一行是一个整数N,表示数据的组数。每组数据占一行,代表四张卡片上的数字(保证四个数字都不同,且0<数字<10)。

Output

对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔,每组输出数据间空一行,最后一组数据后面没有空行。

Sample Input

21 2 3 41 2 3 5

Sample Output

1234 1243 1324 1342 1423 14322134 2143 2314 2341 2413 24313124 3142 3214 3241 3412 34214123 4132 4213 4231 4312 43211235 1253 1325 1352 1523 15322135 2153 2315 2351 2513 25313125 3152 3215 3251 3512 35215123 5132 5213 5231 5312 5321

HINT

4张卡片序号为1、2、3、4,按这个从大到小排列,而不是按照组成的数的大小排列


#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;int main(){    int n,i,num,j;    int a[5],b[5];    cin>>n;    for(i=1;i<=n;i++)    {        num=0;        for(j=0;j<4;j++)        {            cin>>a[j];        }        for(j=0;j<4;j++)        {            b[j]=j;        }        do{            num++;            if(num==6)            {                num=0;                for(j=0;j<4;j++)                {                    cout<<a[b[j]];                }                cout<<endl;            }            else            {                for(j=0;j<4;j++)                {                    cout<<a[b[j]];                }                cout<<" ";            }        } while(next_permutation(b,b+4));        if(i!=n)        {            cout<<endl;        }    }} 


0 0
原创粉丝点击