zoukankan      html  css  js  c++  java
  • ACM 排列2

    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(num,num+4)就好好啦
     1 /*
     2     Name: 排列2 
     3     Copyright: 
     4     Author: 
     5     Date: 09/08/17 15:44
     6     Description: 全排列问题 
     7 */
     8 #include<iostream>
     9 #include<algorithm>
    10 using namespace std;
    11 int main()
    12 {
    13     int num[5],tag;
    14     tag = 0;  //这里的初始化很有必要不然会出现一些很是奇怪的事情 
    15     while(cin>>num[0]>>num[1]>>num[2]>>num[3])
    16     {
    17         if(num[0]==0 && num[1] == 0 && num[2] == 0 && num[3]== 0)
    18             break;
    19         if(tag)
    20             cout<<endl;
    21         tag = 1;
    22         int temp = 1;
    23         int temp1;
    24         do{
    25             if(num[0] == 0)
    26                 continue; 
    27             if(temp)
    28                 cout<<num[0]<<num[1]<<num[2]<<num[3];
    29             else if(temp1 == num[0])
    30                 cout<<" "<<num[0]<<num[1]<<num[2]<<num[3];
    31             else
    32                 cout<<endl<<num[0]<<num[1]<<num[2]<<num[3];
    33             temp = 0;
    34             temp1 = num[0];
    35         }while(next_permutation(num,num+4));
    36         cout<<endl;
    37     } 
    38     return 0;
    39 }
  • 相关阅读:
    Win8系统 Python安装
    一些安卓开源框架整理
    Android 媒体键监听以及模拟媒体键盘的实现 demo
    android View 自动 GONE 问题
    Android 定时器TimerTask 简单使用
    关于Android studio 相对 eclipse 优点
    Java序列化与反序列化
    android shape的使用 边框
    Android Studio 修改 包名 package name
    Android WebView Long Press长按保存图片到手机
  • 原文地址:https://www.cnblogs.com/jj81/p/7326047.html
Copyright © 2011-2022 走看看