输出自然数1~n所有不重复的排列即n的全排列要求所产生的任一数字序列中不允许出现重复数字。输入格式 1=n=9输出格式 由1~n组成的所有不重复的数字序列。每行一个序列输入样例3输出样例1 2 31 3 22 1 32 3 13 1 23 2 1#includebitsstdc++husing namespace std;int na10b10;void dfsint x ifx==n
#include<bits/stdc++.h>
using namespace std;
int n,a[10],b[10];
void dfs(int x){
if(x==n+1){
for(int i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;//每个排列后面要换行
return;
}
for(int i=1;i<=n;i++){
if(b[i]==0){
b[i]=1;
a[x]=i;
dfs(x+1);
b[i]=0;
a[x]=0;
}
}
}
int main(){
cin>>n;
dfs(1);
return 0;
原文地址: https://www.cveoy.top/t/topic/evdT 著作权归作者所有。请勿转载和采集!