zoukankan      html  css  js  c++  java
  • 排列2(全排列next_permutation 注意格式)

    排列2

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7993    Accepted Submission(s): 2967


    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
    题解:千位相同在一行,每两组数据间空一行,最后一组数据没空行。全排列自动去重了;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    int main(){
        int a[4];
        int flot = 0;
        while(1){
            
            int x = 0;
            for(int i = 0; i < 4; i++){
                scanf("%d", a + i);
                x |= a[i];
            }
            sort(a, a + 4);
            if(!x)break;
            
            if(flot)puts("");
            flot ++;
            int p = a[0];
            bool kg = false; 
            do{
                if(a[0] == 0)continue;
                if(a[0] == p){
                    if(kg)printf(" ");
                }else{
                    if(p)puts("");
                    p = a[0];
                    
                }
                kg = true;
                
                
                for(int j = 0; j < 4; j++){
                    printf("%d", a[j]);
                }
                
            }while(next_permutation(a, a + 4));
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    MySQL查看表占用空间大小(转)
    Nginx搭建flv视频点播服务器
    Nginx SPDY Pagespeed模块编译——加速网站载入
    RAD,V模型
    java运算符的优先级
    union和union all的区别
    java数据的5种存储位置(转)
    java对象的初始化过程和创建对象的几种方式
    java异常的原理以及应用
    缓冲与缓存的概念(转)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/6622527.html
Copyright © 2011-2022 走看看