N digital inputs, it outputs all permutations
【answer】
The overall idea is to generate a number of aligned
①-> from the back of the n-1 digits a number picked to replace it on the first position (n-1) th program.
② arranged behind the generation number n-1.
This step is repeated two it
(In Flag feeling than before that [] array method useful method)
[Code]
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10;
int n;
int a[N+10];
int cnt = 0;
void dfs(int k,int m){
if (k==m+1){
cout<<++cnt<<":";
for (int i = 1;i <= m;i++){
cout<<a[i]<<' ';
}
cout<<endl;
return;
}
for (int i = k;i <= m;i++){
swap(a[k],a[i]);
dfs(k+1,m);
swap(a[k],a[i]);
}
}
int main(){
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
dfs(1,n);
return 0;
}