fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string arr;
  7. bool input[9];
  8. int n, m;
  9.  
  10. void solution(int position)
  11. {
  12. if (position == m)
  13. {
  14. for (int i = 0; i < m; i++)
  15. cout << arr[i] << " ";
  16. cout << '\n';
  17. return;
  18. }
  19.  
  20. for (int i = 1; i <= n; i++)
  21. {
  22. if (!input[i])
  23. {
  24. input[i] = true;
  25. arr.push_back(i + '0');
  26.  
  27. solution(position + 1);
  28.  
  29. input[i] = false;
  30. arr.pop_back();
  31. }
  32. }
  33. }
  34.  
  35. int main(void)
  36. {
  37. cin >> n >> m;
  38. solution(0);
  39. return 0;
  40. }
Success #stdin #stdout 0s 4772KB
stdin
4 4
stdout
1 2 3 4 
1 2 4 3 
1 3 2 4 
1 3 4 2 
1 4 2 3 
1 4 3 2 
2 1 3 4 
2 1 4 3 
2 3 1 4 
2 3 4 1 
2 4 1 3 
2 4 3 1 
3 1 2 4 
3 1 4 2 
3 2 1 4 
3 2 4 1 
3 4 1 2 
3 4 2 1 
4 1 2 3 
4 1 3 2 
4 2 1 3 
4 2 3 1 
4 3 1 2 
4 3 2 1