Problem Solving/BOJ

[Silver 3] 15650번 N과 M (2)

kmkunk 2022. 4. 1. 14:38
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));
        sb = new StringBuilder();
        String[] arr = br.readLine().split(" ");
        int n = Integer.parseInt(arr[0]); int m = Integer.parseInt(arr[1]);
        ans = new int[m];
        
        calc(1, 0, n, m);
        
        System.out.println(sb);
    }
    
    public static void calc(int index, int selected, int n, int m) {
        if(selected==m) {
            for(int a : ans) {
                sb.append(a).append(" ");
            }
            
            sb.append("\n");
            return;
        }
        
        if(index>n) { return; }
        
        ans[selected] = index;
        calc(index+1, selected+1, n, m);
        calc(index+1, selected, n, m);
    }
}