HDU-1716 Arrangement 2 (full arrangement + cheating output format)

First come the bleak picture of the last Presentation Error:

Because of the wrong output format, it took two hours to debug, during which I observed other bloggers' writing and finally AC

 

topic portal

permutation 2

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


 

Problem Description

Ray is also interested in the column of numbers:
there are four cards, and many different 4-digit numbers can be arranged with these four cards, and it is required to output these 4-digit numbers in ascending order.

 

 

Input

Each set of data occupies one line, representing the numbers on the four cards (0<=number<=9). If the four cards are all 0, the input ends.

 

 

Output

For each group of cards, output all 4-digit numbers that can be composed of these four cards in order from small to large. The same thousand-digit number is on the same line, and each four-digit number in the same line is separated by a space.
There is a blank line between each set of output data, and there is no blank line after the last set of data.

 

 

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

 

 

思路:

一看就能想到用全排列做,将所有的情况枚举出来然后再输出

注意:

1.使用全排列之前需要数组用sort以字典序升序排好,这样可以保证全排列可以枚举出所有的情况

2.while(next_permutation(a,a+4))  和 do{ }while(next_permutation(a,a+4));  是有很大区别的

前者不能输出第一种最小的情况,所以我们需要在while前手动把第一种情况给输出来

后者则可以输出所有的情况

3.坑人的输出格式:

每次都要判断下一次输入是否会退出,如果退出则少输出一个换行符

 

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int map[5];



int main()
{
	scanf("%d %d %d %d",&map[0],&map[1],&map[2],&map[3]);
	while(1)
	{
		if(!map[0]&&!map[1]&!map[2]&&!map[3])
		{
			break;
		}
		sort(map,map+4);   //字典序升序
		int flag=map[0];   //记录千位数数字 
		if(map[0]!=0)
		{
                    printf("%d%d%d%d",map[0],map[1],map[2],map[3]);
                }
		while( next_permutation(map,map+4) )   //使用while输出全排列函数,则第一种可能不会出现,需要手动输出 
		{					 //使用do while则所有可能都会输出 
			if(map[0]==flag && map[0]!=0)
				printf(" %d%d%d%d",map[0],map[1],map[2],map[3]);
			else
			{
				if(map[0]!=flag)
				{
					if(flag!=0)
					{
						printf("\n");
					}
					printf("%d%d%d%d",map[0],map[1],map[2],map[3]);
					flag=map[0];		
				}	
			}	
		}
		printf("\n");
		scanf("%d %d %d %d",&map[0],&map[1],&map[2],&map[3]);  //再一次输入 
		if(!map[0]&&!map[1]&!map[2]&&!map[3])	
				break;
		else
			printf("\n");       //下一次输入不都为0,输出空行 
	}
	return 0;
} 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326282272&siteId=291194637