1 #include "iostream" 2 #include "vector" 3 using namespace std; 4 5 void print(vector<int>Array) 6 { 7 int i; 8 for(i=0;i<Array.size();i++) 9 { 10 cout<<Array[i]<<" "; 11 } 12 cout<<endl; 13 } 14 15 void swap(vector<int>&Array,int i,int j) 16 { 17 int temp; 18 temp = Array[i]; 19 Array[i] = Array[j]; 20 Array[j] = temp; 21 } 22 23 void backtrack(int i,vector<int>Array) 24 { 25 if(i>=Array.size()) 26 { 27 print(Array); 28 } 29 for(int j=i;j<Array.size();j++) 30 { 31 swap(Array,i,j); 32 backtrack(i+1,Array); 33 swap(Array,i,j); 34 } 35 } 36 37 int main() 38 { 39 vector<int>Array; 40 int N,i; 41 cout<<"please input num N:N="; 42 cin>>N; 43 for(i=0;i<N;i++) 44 { 45 Array.push_back(i+1); 46 } 47 backtrack(0,Array); 48 49 return 0; 50 }