Problem Solving/BOJ

[Silver 3] 15651번 N과 M (3)

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(0, n, m);
        System.out.println(sb);
    }
    
    public static void calc(int index, int n, int m) {
        if(index==m) {
            for(int a : ans) {
                sb.append(String.valueOf(a)).append(" ");
            }
            
            sb.append("\n");
            return;
        }
        
        for(int i=0; i<n; i++) {
            ans[index] = i+1;
            calc(index+1, n, m);
        }
    }
}