fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, k;
  5. int a[10];
  6. int use[10];
  7. void go(int depth) {
  8. if (k == depth) {
  9. for (int i = 0; i < k; i++) cout << a[i] << " ";
  10. cout << '\n';
  11. }
  12.  
  13. for (int i = 1; i <= n; i++) {
  14. if (use[i] >= k) continue;
  15. a[depth] = i;
  16. use[i]++;
  17. go(depth + 1);
  18. use[i]--;
  19. }
  20. }
  21. int main() {
  22. ios_base::sync_with_stdio(0);
  23. cin.tie(0);
  24. cin >> n >> k;
  25.  
  26. go(0);
  27. }
Success #stdin #stdout 0s 5300KB
stdin
5 3
stdout
1 1 1