PTA full permutation
full permutation
there is an integer n,that means there are n numbers from 1 to n,then output the full permutation about the first n nature number.
input specification:
input one integer n.
output specification:
list the full permutation about the first n numbers with dictionary sequence,each permutation as one line.
input example:
3
output:
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
code:
#include<bits/stdc++.h>
using namespace std;
int n,x[100];
bool vis[100];
void f(int t){
if(t>n){
for(int i=1;i<=n;i++)
cout<<x[i]<<" ";
cout<<endl;
}
else{
for(int i=1;i<=n;i++)
if(vis[i]==0){
x[t]=i;
vis[i]=1;
f(t+1);
vis[i]=0;
}
}
}
int main()
{
cin>>n;
f(1);
return 0;
}

浙公网安备 33010602011771号