fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. bool Perm(int m){
  6. std::function<void(int, std::vector<int>)> PermSub = [&](int n, std::vector<int> a){
  7. bool IsFind = false;
  8. if (n == m){
  9. std::cout << '[';
  10. for (auto& o : a) std::cout << o << ',';
  11. std::cout << ']';
  12. std::cout << std::endl;
  13. }
  14. else{
  15. for (int x = 2; x < m + 1; x++){
  16. IsFind = false;
  17. for (auto& i : a){
  18. if (x == i){
  19. IsFind = true;
  20. break;
  21. }
  22. }
  23. if (IsFind == false){
  24. if (n != 2 || a[0]>x){
  25. a.push_back(x);
  26. PermSub(n + 1, a);
  27. a.pop_back();
  28. }
  29. }
  30. }
  31. }
  32. };
  33.  
  34. for (int j = 2; j < m + 1; j++) PermSub(2, { j, 1 });
  35.  
  36. return true;
  37. }
  38.  
  39. int main(){
  40.  
  41. Perm(5);
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
[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,]