挑战题之排列生成

挑战题之排列生成

Time Limit:   2000MS       Memory Limit:   65535KB
Submissions:   435       Accepted:   170
Description

一自然数N,设N为3,则关于N的字典序排列为123,132,213,231,312,321。对于一个自然数N(1<= N <= 9 ) , 你要做的便是生成它的字典序排列。

Input

第一行为自然数N。

Output

输出对应于N的字典序排列,每个排列占一行。

Sample Input

3

Sample Output

123
132
213
231
312
321
#include<iostream>
#include <cstdio>
using namespace std;
int s[10];
int s1[10];
int vis[10] = {0};
void dfs(int l,int n )
{
	int i;
	if(l>=n)
	{
		for(i= 0;i<n;i++)
			printf("%d",s1[i]);
			printf("
");
	}
	for(i=0;i<n;i++)
	{
		if(!vis[i])
		{
			vis[i] = 1;
		s1[l] = s[i];
		dfs(l+1,n);
	    vis[i] = 0;
		}
	}

}
int main()
{
	int num;
	cin>>num;
	for(int i=0;i<num;i++)
		s[i] = i+1;
    dfs (0, num);
	return 0;
}

  

原文地址:https://www.cnblogs.com/locojyw/p/3704851.html