fork download
  1. #include <iostream>
  2. using namespace std;
  3. int N,M;
  4. char s[20]={0};
  5. void permute(int x, int cnt) {
  6. if (cnt==M) {
  7. cout << s << "\n";
  8. return;
  9. }
  10. for (int y=x;y<=N;y++) {
  11. s[2*cnt]=y+'0';
  12. s[2*cnt+1]=' ';
  13. permute(y, cnt+1);
  14. }
  15. }
  16.  
  17. int main() {
  18. cin.tie(0)->sync_with_stdio(0);
  19. cin >> N >> M;
  20. permute(1, 0);
  21. }
Success #stdin #stdout 0.01s 5280KB
stdin
4 2
stdout
1 1 
1 2 
1 3 
1 4 
2 2 
2 3 
2 4 
3 3 
3 4 
4 4