21.09.01 기록

최대 1 분 소요

백준 알고리즘 15650 풀이

🎆해설(메모리 14.2MB, 시간 140ms로 통과)

-지난번 해설을 응용하여 풀이해보려 했으나, 풀지못했다.😥
-매개변수를 하나 더 사용하면 간단히 풀 수 있는 문제였다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B15650 {

    public static int N;
    public static int M;
    public static int[] nums;
    public static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        nums = new int[M];

        dfs(1, 0);
        System.out.println(sb);
    }

    public static void dfs(int start, int depth) {
        if(depth == M) {
            for(int i = 0; i < M; i++) {
                sb.append(nums[i]).append(" ");
            }
            sb.append("\n");
            return;
        }

        for(int i = start; i <= N; i++) {
            nums[depth] = i;
            dfs(i+1, depth+1);
        }
    }
}

카테고리:

업데이트: