zoukankan      html  css  js  c++  java
  • 杭电1716--排列2

    排列2

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


    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
     

     

    Source
     

     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1719 1717 1521 1112 1796 
    。RT 。: 感觉和组合数差不多, 多了个去重, 多了个格式。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 int main()
     7 {
     8     int a, b, c, d, num[4], deal[25], j = 0;
     9     while(~scanf("%d %d %d %d", &a, &b, &c, &d), a+b+c+d)
    10     {
    11         if(j != 0)
    12             printf("
    ");
    13         int m  = 0;
    14         num[0] = a; num[1] = b; num[2] = c; num[3] = d;
    15         for(int i = 0; i < 4; i++)
    16             for(int j  = 0; j < 4; j++)
    17                 for(int k = 0; k < 4; k++)
    18                     for(int l = 0; l < 4 ; l++)
    19                         if(num[i] != 0 && i != j && i != k && i != l && j != k && j != l && k != l)  //24种组合方式。 
    20                             deal[m++] = num[i]*1000 + num[j]*100 + num[k]*10 + num[l];
    21                         //    printf("%d
    ", m);
    22     
    23         sort(deal, deal + m);
    24         int ti = deal[0];
    25         for(int i = 1; i < m; i++)   //去重; 
    26         {
    27             if(deal[i] == ti)
    28                 deal[i] = 0;
    29             else
    30                 ti = deal[i];
    31         }
    32         int temp = deal[0]/1000;
    33         printf("%d", deal[0]);
    34         for(int i = 1; i < m; i++)
    35         {
    36             if(!deal[i])
    37                 continue;
    38             if(deal[i]/1000 == temp)
    39                 printf(" %d", deal[i]);
    40             else    
    41             {
    42                 temp = deal[i] / 1000;
    43                 printf("
    ");
    44                 printf("%d", deal[i]);
    45             }
    46         }
    47         printf("
    ");
    48         j++;
    49     }
    50     return 0;
    51 }
     
  • 相关阅读:
    7行代码看EntityFramework是如何运行
    我用ASP.NET缓存之SQL数据缓存依赖(SqlCacheDependency)
    利用Microsoft.Office.Interop.Excel 将web页面转成PDF
    IT农民的开发人员工具清单(2013年)
    我在项目中运用 IOC(依赖注入)--实战篇
    我在项目中运用 IOC(依赖注入)--入门篇
    我用ASP.NET缓存之数据缓存
    我用ASP.NET缓存之OutputCache
    Resharper 使用帮助-自动生成文件头
    玩转变量、环境变量以及数学运算(shell)
  • 原文地址:https://www.cnblogs.com/soTired/p/4725356.html
Copyright © 2011-2022 走看看