Hdoj1716排列2

Hdoj1716:

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
 

用STL就簡單了,不過格式挺麻煩的。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[4];
int main()
{
//    freopen("a.txt","r",stdin);
    int k=0;
    while(cin>>a[0]>>a[1]>>a[2]>>a[3])
    {
        if(!(a[0]||a[1]||a[2]||a[3]))
            break;
        if(k)
            cout<<'\n';
        k++;
        sort(a,a+4);
        int n=0,tmp;
        do{
            if(n){
                if(a[0]==tmp)
                    cout<<' ';
                else
                    cout<<'\n';
            }
            if(a[0]){
                n++;
                for(int i=0;i<4;i++)
                    cout<<a[i];
            }
            tmp=a[0];
        }while(next_permutation(a,a+4));
        cout<<'\n';
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章