zoukankan      html  css  js  c++  java
  • HDU 1716 全排列

    排列2

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


    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函数过,就是格式有点恶心。
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <string.h>
     5 using namespace std;
     6 const int N = 5;
     7 int a[N];
     8 int main() {
     9     int n, m, k = 1, ans;
    10     while(cin>>a[0]>>a[1]>>a[2]>>a[3]) {
    11         if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0) break;
    12         if(k != 1) printf("
    ");
    13         int flag = 1, ans;
    14         do{
    15             if(a[0] == 0)continue;
    16             if(flag) flag = 0;
    17             else if(ans == a[0]) printf(" ");
    18             else printf("
    ");
    19             printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
    20             ans = a[0];
    21         }while(next_permutation(a, a+4));
    22         printf("
    ");
    23         k++;
    24     }
    25     return 0;
    26 }
  • 相关阅读:
    print格式化输出(format)
    Python list,tuple,dict,set高级变量常用方法
    K8s的kubectl常用命令
    C/C++中数组与指针的关系探究
    Java中的不可变类
    JAVA 类总结
    c++ string类find总结
    关于看板娘的事儿
    类型转换
    计算机2进制小数点表示法
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7285899.html
Copyright © 2011-2022 走看看