[백준][15651번] N과 M(3)

Updated:

문제 URL

https://www.acmicpc.net/problem/15651 boj15651

내풀이

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static int[] ans;

    public static void main(String[] args) throws IOException {
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        ans = new int[m];
        cal(0, n, m);
        bw.flush();
        bw.close();
    }

    public static void cal(int idx, int n, int m) throws IOException {
        if (idx == m) {
            StringBuilder sb = new StringBuilder();
            for (int a : ans)
                sb.append(a + " ");
            bw.write(sb.toString());
            bw.newLine();
            return;
        }

        for (int i = 1; i <= n; i++) {
            ans[idx] = i;
            cal(idx + 1, n, m);
        }
    }
}

Leave a comment