排列2(HDU-1716)

Problem Description

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

Input

每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。

Output

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

Sample Input

1 2 3 4
1 1 2 3
0 1 2 3
0 0 0 0

Sample Output

1234 1243 1324 1342 1423 1432
2134 2143 2314 2341 2413 2431
3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321

1123 1132 1213 1231 1312 1321
2113 2131 2311
3112 3121 3211

1023 1032 1203 1230 1302 1320
2013 2031 2103 2130 2301 2310
3012 3021 3102 3120 3201 3210

思路:对于每组给出的数,直接用 next_permutation() 求全排列即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 3000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

int a[N];
int main(void) {
    int Case=0;
    while(~scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])) {
        if(a[0]+a[1]+a[2]+a[3]==0)
            break;

        sort(a,a+4);

        if(Case!=0)
            printf("\n");
        Case++;

        int now=a[0];
        int i=0;
        while(a[i]==0)
            now=a[++i];

        bool flag=true;
        do {
            if(a[0]==0) {
                continue;
            }
            if(flag) {
                printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
                flag=false;
            }
            else if(a[0]==now) {
                printf(" %d%d%d%d",a[0],a[1],a[2],a[3]);
            }
            else {
                printf("\n%d%d%d%d",a[0],a[1],a[2],a[3]);
            }
            now=a[0];
        } while(next_permutation(a,a+4));
        printf("\n");
    }
    return 0;
}

 

posted @   老程序员111  阅读(122)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示