21.09.04 기록

최대 1 분 소요

백준 알고리즘 15651 풀이

🎆나의 풀이(메모리 68MB, 시간 488ms로 통과)

-이 문제도 지난 문제인 #15649를 응용하여 풀이했다.
-중복을 허용하기 때문에 boolean 배열을 사용하지 않는 것 말고는 다른 것이 없는 듯 하다.
-해설 역시 같은 내용이라 따로 내용을 추가하지 않았다!

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

public class B15651 {

    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(0);
        System.out.println(sb);
    }

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

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


카테고리:

업데이트: