HDU - 1716
Description
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。 Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。 Sample Input
Sample Output
Source
2007省赛集训队练习赛(2)
|
1 本题用到一个C++里的全排列函数nest_permutation(a,a+n)
它的特点是可以输出所有的序列,而且必须先用sort排序,因为它只找比自己大序列
它必须先输出第一组,然后才能用while去找下一组,也可以用do while直接输出
2 本题对格式要求特别多,需要保持一个清醒的头脑去做题,用标记法去控制每个空行或空格
用continue,break,去终止本次循环,或者跳出循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int a[5],i;
int k=0;
while(1)
{
for(i=0; i<4; i++)
scanf("%d",&a[i]);
sort(a,a+4);
if(a[0]!=0||a[1]!=0||a[2]!=0||a[3]!=0)
{
if(k!=0)
printf("\n");
k++;
if(a[0]!=0)
{
for(i=0; i<4; i++)
printf("%d",a[i]);
}
int t=a[0];
while(next_permutation(a,a+4))
{int ok=1;
if(a[0]==0)
continue;
if(a[0]!=t)
{
ok=0;
if(t!=0)
printf("\n");
t=a[0];
}
if(ok==1)
printf(" ");
for(i=0; i<4; i++)
printf("%d",a[i]);
}
printf("\n");
}
else
break;
}
return 0;
}