欢迎来到PJCK的博客

(next_permutation) 排列2 hdu 1716

排列2
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9588    Accepted Submission(s): 3486
 
 
Problem Description
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
 
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
 
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。
 
Sample Input
1 2 3 41 1 2 30 1 2 30 0 0 0
 
Sample Output
1234 1243 1324 1342 1423 14322134 2143 2314 2341 2413 24313124 3142 3214 3241 3412 34214123 4132 4213 4231 4312 43211123 1132 1213 1231 1312 13212113 2131 23113112 3121 32111023 1032 1203 1230 1302 13202013 2031 2103 2130 2301 23103012 3021 3102 3120 3201 3210
 
 
 
 
 
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[5],tag=0;
while(cin>>a[0]>>a[1]>>a[2]>>a[3],a[0]||a[1]||a[2]||a[3])
{
if(tag)
cout<<endl; //因为最后一组数据没有空行。所以用这个方法。
tag=1;
int flag=1,tmp;
do
{
if(a[0]==0)
continue; //审清例子,可知千位数没有0。
if(flag)
{
cout<<a[0]<<a[1]<<a[2]<<a[3];
flag=0;
}
else if(tmp==a[0])
cout<<" "<<a[0]<<a[1]<<a[2]<<a[3];
else
cout<<"\n"<<a[0]<<a[1]<<a[2]<<a[3];
tmp=a[0];
}while(next_permutation(a,a+4)); //扎实基础。不能写成(a,a+3)。
cout<<endl;
}
return 0;
}
 

 

posted @ 2018-02-20 19:53  PJCK  阅读(183)  评论(0编辑  收藏  举报