fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <algorithm>
  5.  
  6. std::vector<std::vector<int>> Perm(int m){//this method is not my idea.i am confusing.
  7.  
  8. std::vector<std::vector<int>> vecvec;
  9.  
  10. std::function<void(int, std::vector<int>)> PermSub = [&](int n, std::vector<int> a){
  11. bool IsFind = false;
  12. if (n == m){
  13. std::rotate(a.begin(), a.begin() + 1, a.end());//rotate for left.fix the method spec.
  14. vecvec.push_back(a);
  15. }
  16. else{
  17. for (int x = 1; x < m; x++){
  18. IsFind = false;
  19. for (auto& i : a){
  20. if (x == i){
  21. IsFind = true;
  22. break;
  23. }
  24. }
  25. if (IsFind == false){
  26. if (n != 2 || a[0]>x){
  27. a.push_back(x);
  28. PermSub(n + 1, a);
  29. a.pop_back();
  30. }
  31. }
  32. }
  33. }
  34. };
  35.  
  36. for (int j = 1; j < m; j++) PermSub(2, { j, 0});
  37.  
  38. return vecvec;
  39. }
  40. template<class T>
  41. bool MakeHoge(int M,T Base=T()){
  42. auto R = Perm(M);
  43.  
  44. std::cout << "first:" << M << " Count!"<<std::endl;
  45.  
  46. for (auto& oo : R){
  47. std::cout << '[';
  48. for (auto& o : oo) std::cout << static_cast<T>(o+Base) << ',';
  49. std::cout << ']';
  50. std::cout << std::endl;
  51. }
  52. std::cout << std::endl;
  53. std::cout << std::endl;
  54. return true;
  55. }
  56. template<class T>
  57. bool MakeHoge(const std::vector<T>& vec){
  58. auto R = Perm(vec.size());
  59.  
  60. std::cout<<"First:" << '[';
  61. for (auto& o : vec) std::cout << o << ',';
  62. std::cout << ']';
  63. std::cout << std::endl;
  64. for (auto& oo : R){
  65. std::cout << '[';
  66. for (auto& o : oo) std::cout << vec[o] << ',';
  67. std::cout << ']';
  68. std::cout << std::endl;
  69. }
  70. std::cout << std::endl;
  71. std::cout << std::endl;
  72. return true;
  73. }
  74.  
  75. int main(){
  76.  
  77. MakeHoge<int>({ 1, 2, 3, 4, 5 });
  78. MakeHoge<char>({ 'a', 'B', 'c', 'D','e' });
  79. MakeHoge<int>(5);
  80. MakeHoge<char>(5, 'a');
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
First:[1,2,3,4,5,]
[1,2,4,5,3,]
[1,2,5,4,3,]
[1,2,3,5,4,]
[1,2,5,3,4,]
[1,3,2,5,4,]
[1,3,5,2,4,]
[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,5,]


First:[a,B,c,D,e,]
[a,B,D,e,c,]
[a,B,e,D,c,]
[a,B,c,e,D,]
[a,B,e,c,D,]
[a,c,B,e,D,]
[a,c,e,B,D,]
[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,e,]


first:5 Count!
[0,1,3,4,2,]
[0,1,4,3,2,]
[0,1,2,4,3,]
[0,1,4,2,3,]
[0,2,1,4,3,]
[0,2,4,1,3,]
[0,1,2,3,4,]
[0,1,3,2,4,]
[0,2,1,3,4,]
[0,2,3,1,4,]
[0,3,1,2,4,]
[0,3,2,1,4,]


first:5 Count!
[a,b,d,e,c,]
[a,b,e,d,c,]
[a,b,c,e,d,]
[a,b,e,c,d,]
[a,c,b,e,d,]
[a,c,e,b,d,]
[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,e,]