方法1:DFS
#include<bits/stdc++.h>
using namespace std;
int n,ans[15],use[15];
inline void dfs(int x){
if(x>n){
for(int i=1;i<=n;++i){
printf("%5d",ans[i]);
}
puts("");
return;
}
for(int i=1;i<=n;i++)
if(!use[i]){
ans[x]=i;
use[i]=1;
dfs(x+1);
use[i]=0;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
dfs(1);
return 0;
}
方法2:主函数递归
#include<bits/stdc++.h>
using namespace std;
bool vis[15];
int n,step=1,a[15];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if(step==1) cin>>n;
if(step-1==n){
for(int i=1;i<=n;++i){
printf("%5d",a[i]);
}
puts("");
}
for(int i=1;i<=n;++i){
if(vis[i]==0){
vis[i]=1;
a[step]=i;
step++;
main();
step--;
vis[i]=0;
}
}
return 0;
}
方法3:不定层数循环
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,x=1,s=0,a[1001];
bool b[1001];
cin>>n;
m=n;
for(int i=1;i<=n;++i){
b[i]=false;
}
for(int i=1;i<=m;++i){
a[i]=i;
b[i]=true;
}
while(x>0){
s++;
for(int i=1;i<=m;++i){
cout<<setw(5)<<a[i];
}
cout<<"\n";
x=m;
while(x>0){
b[a[x]]=false;
int i;
for(i=a[x]+1;i<=n;++i){
if(b[i]==false){
a[x]=i;
b[i]=true;
break;
}
}
if(i<=n) break;
x--;
}
if(x>0){
int cnt=1;
for(int i=1;x+cnt<=m&&i<=n;++i){
if(b[i]==false){
a[x+cnt]=i;
b[i]=true;
cnt++;
}
}
}
}
return 0;
}
方法4:状压
#include<bits/stdc++.h>
using namespace std;
int n,lg[5001],ans[11];
inline void dfs(int x,int s){
if(x>n){
for(int i=1;i<=n;++i){
printf("%5d",ans[i]);
}
puts("");
return ;
}
for(int ss=s;ss>0;ss-=ss&(-ss)){
int temp=ss&(-ss);
ans[x]=lg[temp];
dfs(x+1,s-temp);
}
}
int main(){
cin>>n;
lg[1]=1;
for(int i=2;i<=n;++i){
lg[1<<(i-1)]=i;
}
dfs(1,(1<<n)-1);
return 0;
}
方法5:STL大法好
#include<bits/stdc++.h>
using namespace std;
int n,a[11];
int main(){
cin>>n;
for(int i=1;i<=n;++i){
a[i]=i;
}
do{
for(int i=1;i<=n;++i){
cout<<setw(5)<<a[i];
}
puts("");
}while(next_permutation(a+1,a+n+1));
return 0;
}
方法6:枚举骗分
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10];
for(int i=1;i<=9;++i){
a[i]=i;
}
int n;
cin>>n;
for(int i1=1;i1<=n;++i1){
if(n==1){
cout<<" "<<i1<<"\n";
continue;
}
for(int i2=1;i2<=n;++i2){
if(i2==i1) continue;
if(n==2){
cout<<" "<<i1<<" "<<i2<<"\n";
continue;
}
for(int i3=1;i3<=n;++i3){
if(i3==i2||i3==i1) continue;
if(n==3){
cout<<" "<<i1<<" "<<i2<<" "<<i3<<"\n";
continue;
}
for(int i4=1;i4<=n;++i4){
if(i4==i3||i4==i2||i4==i1) continue;
if(n==4){
cout<<" "<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<"\n";
continue;
}
for(int i5=1;i5<=n;++i5){
if(i5==i4||i5==i3||i5==i2||i5==i1) continue;
if(n==5){
cout<<" "<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<" "<<i5<<"\n";
continue;
}
for(int i6=1;i6<=n;++i6){
if(i6==i5||i6==i4||i6==i3||i6==i2||i6==i1) continue;
if(n==6){
cout<<" "<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<" "<<i5<<" "<<i6<<"\n";
continue;
}
for(int i7=1;i7<=n;++i7){
if(i7==i6||i7==i5||i7==i4||i7==i3||i7==i2||i7==i1) continue;
if(n==7){
cout<<" "<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<" "<<i5<<" "<<i6<<" "<<i7<<"\n";
continue;
}
for(int i8=1;i8<=n;++i8){
if(i8==i7||i8==i6||i8==i5||i8==i4||i8==i3||i8==i2||i8==i1) continue;
if(n==8){
cout<<" "<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<" "<<i5<<" "<<i6<<" "<<i7<<" "<<i8<<"\n";
continue;
}
for(int i9=n;i9<=n;++i9){
if(i9==i8||i9==i7||i9==i6||i9==i5||i9==i4||i9==i3||i9==i2||i9==i1) continue;
cout<<" "<<i1<<" "<<i2<<" "<<i3<<" "<<i4<<" "<<i5<<" "<<i6<<" "<<i7<<" "<<i8<<" "<<i9<<"\n";
}
}
}
}
}
}
}
}
}
return 0;
}
双倍经验(AcWing 823)
观此题解小生不胜荣幸,请顺手留赞
求关注,关注必回关