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