http://bailian.openjudge.cn/practice/4070
next_permutation();
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int a[10];
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin >> n,n)
{
for(int i=0;i<n;i++)
a[i]=i+1;
do
{
for(int i=0;i<n;i++)
printf("%d%c",a[i],i==n-1?'\n':' ');
}while(next_permutation(a,a+n));
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int a[10],n,b[10],vis[10];
void dfs(int step)
{
if(step>=n)
{
for(int i=0;i<n;i++)
printf("%d%c",b[i],i==n-1?'\n':' ');
}
for(int i=0;i<n;i++)
{
if(vis[i]==0)
{
vis[i]=1;
b[step]=a[i];
dfs(step+1);
vis[i]=0;
}
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> n,n)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
a[i]=i+1;
dfs(0);
}
return 0;
}
来源:CSDN
作者:AimerAimerAimer
链接:https://blog.csdn.net/qq_43032263/article/details/104879406