#include <bits/stdc++.h>
using namespace std;

int n,m;
int arr[10];
bool is_used[10];
/*void NM(int now_n,int last) {
    if(now_n==m) {
        for(int i=0; i<m; i++) {
            cout << arr[i] << ' ';
        }
        cout <<'\n';
        return;
    }
    for(int i=last; i<=n; i++) {
        arr[now_n]=i;
        NM(now_n+1,i+1);
    }
}*/
void NM(int now_n) {
    //base condition
    if(now_n==m){
        for(int i=0; i<m; i++) {
            cout << arr[i] << ' ';
        }
        cout <<'\n';
        return;
    }
    //다음수 찾기 
    for(int i=1; i<=n; i++) {
        //i가 사용이 안됨
        if(!is_used[i]) {
            //처음이거나 이전의 수보다 i 가 클때 백트래킹
            if(now_n==0 || arr[now_n-1]<i) {
                arr[now_n]=i;
                is_used[i]=1;
                NM(now_n+1);
                is_used[i]=0;
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    NM(0);
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: