fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. std::vector<std::vector<int>> Perm(int m){//this method is not my idea.i am confusing.
  6.  
  7. std::vector<std::vector<int>> vecvec;
  8.  
  9. std::function<void(int, std::vector<int>)> PermSub = [&](int n, std::vector<int> a){
  10. bool IsFind = false;
  11. if (n == m){
  12. vecvec.push_back(a);
  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 vecvec;
  37. }
  38. template<class T>
  39. bool MakeHoge(int M,T Base=T()){
  40. auto R = Perm(M);
  41.  
  42. if (Base != 0) Base--;
  43.  
  44. for (auto& oo : R){
  45. std::cout << '[';
  46. for (auto& o : oo) std::cout << static_cast<T>(o+Base) << ',';
  47. std::cout << ']';
  48. std::cout << std::endl;
  49. }
  50. std::cout << std::endl;
  51. std::cout << std::endl;
  52. return true;
  53. }
  54.  
  55. int main(){
  56. MakeHoge<int>(5);
  57. MakeHoge<char>(5, 'a');
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 3432KB
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,]


[c,a,b,d,e,]
[c,a,b,e,d,]
[d,a,b,c,e,]
[d,a,b,e,c,]
[d,a,c,b,e,]
[d,a,c,e,b,]
[e,a,b,c,d,]
[e,a,b,d,c,]
[e,a,c,b,d,]
[e,a,c,d,b,]
[e,a,d,b,c,]
[e,a,d,c,b,]