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