Problem Solving/BOJ

[Silver 3] 15652번 N과 M (4)

kmkunk 2022. 4. 1. 14:39
import java.io.*;

public class Main {
    public static int[] ans;
    public static StringBuilder sb;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] arr = br.readLine().split(" ");
        int n = Integer.parseInt(arr[0]); int m = Integer.parseInt(arr[1]);
        sb = new StringBuilder();
        ans = new int[m];
        
        calc(0, 0, n, m);
        System.out.println(sb);
    }
    
    public static void calc(int index, int start, int n, int m) {
        if(index==m) {
            for(int a : ans) {
                sb.append(String.valueOf(a)).append(" ");
            }
            
            sb.append("\n");
            return;
        }

        for(int i=start; i<n; i++) {
            ans[index] = i+1;
            calc(index+1, i, n, m);
        }
    }
}