有序全排列c++实现(递归)
1 #include <iostream> 2 #include <algorithm> 3 #include <iterator> 4 #include <vector> 5 using namespace std; 6 int number; 7 vector<int> vec; 8 vector<bool> book; 9 vector<int> ans; 10 void permutation(int k){ 11 if(k > number - 1){ 12 copy(ans.begin(), ans.end(), ostream_iterator<int>(cout, " ")); 13 cout << endl; 14 return; 15 } 16 17 for(int i = 0; i < number; i++){ 18 if(!book[i]){ 19 book[i] = true; 20 ans[k] = vec[i]; 21 permutation(k + 1); 22 book[i] = false; 23 } 24 25 } 26 } 27 int main(){ 28 cin >> number; 29 for(int i = 0; i < number; i++){ 30 vec.push_back(i + 1); 31 book.push_back(false); 32 } 33 ans.assign(number,0); 34 permutation(0); 35 return 0; 36 }