#include <iostream> #include <vector> #include <functional> bool Perm(int m){ std::function<void(int, std::vector<int>)> PermSub = [&](int n, std::vector<int> a){ bool IsFind = false; if (n == m){ std::cout << '['; for (auto& o : a) std::cout << o << ','; std::cout << ']'; std::cout << std::endl; } else{ for (int x = 2; x < m + 1; x++){ IsFind = false; for (auto& i : a){ if (x == i){ IsFind = true; break; } } if (IsFind == false){ if (n != 2 || a[0]>x){ a.push_back(x); PermSub(n + 1, a); a.pop_back(); } } } } }; for (int j = 2; j < m + 1; j++) PermSub(2, { j, 1 }); return true; } int main(){ Perm(5); return 0; }
Standard input is empty
[3,1,2,4,5,] [3,1,2,5,4,] [4,1,2,3,5,] [4,1,2,5,3,] [4,1,3,2,5,] [4,1,3,5,2,] [5,1,2,3,4,] [5,1,2,4,3,] [5,1,3,2,4,] [5,1,3,4,2,] [5,1,4,2,3,] [5,1,4,3,2,]