๊ธฐ๋ก๋ฐฉ

BOJ_15989 : 1, 2, 3 ๋”ํ•˜๊ธฐ 4 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_15989 : 1, 2, 3 ๋”ํ•˜๊ธฐ 4

Soom_1n 2024. 8. 16. 23:30

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • 1, 2, 3์˜ ๋ง์…ˆ์œผ๋กœ n์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ๋กœ, dp[n]์€ n์„ ๋งŒ๋“œ๋Š” ์กฐํ•ฉ์˜ ์ˆ˜์ด๋‹ค.
  • ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด 1๋กœ ํ•œ๋ฐ”ํ€ด, 2๋กœ ํ•œ๋ฐ”ํ€ด, 3์œผ๋กœ ํ•œ๋ฐ”ํ€ด ๊ฐ๊ฐ dp๋ฅผ ๋Œ๋ ค์•ผ ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int[] dp = new int[10_001];
        dp[0] = 1;
        for (int i = 1; i <= 3; i++) {
            for (int j = i; j < 10_001; j++) {
                dp[j] += dp[j - i];
            }
        }

        int T = Integer.parseInt(br.readLine());
        for (int t = 1; t <= T; t++) {
            int n = Integer.parseInt(br.readLine());
            sb.append(dp[n]).append('\n');
        }
        bw.write(sb.toString());
        bw.flush();
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • dp๋ฐฐ์—ด์„ ์ตœ๋Œ“๊ฐ’์ธ 10,000๊นŒ์ง€ ๋จผ์ € ์ฑ„์šด๋‹ค.
    • dp[0] ๋งŒ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ 1์„ ๋„ฃ์–ด์ค€๋‹ค.
    • 1๋กœ ํ•œ๋ฐ”ํ€ด, 2๋กœ ํ•œ๋ฐ”ํ€ด, 3์œผ๋กœ ํ•œ๋ฐ”ํ€ด dp ๋ฐฐ์—ด์„ ์ฑ„์›Œ ์ค‘๋ณต์„ ํ”ผํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

    • ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
    • ์ƒ๊ฐ๋ณด๋‹ค ํ’€์ด ๊ฒฐ๊ณผ๋Š” ๊ฐ„๋‹จํ•ด์„œ ์ด๋Ÿฐ ์ข…๋ฅ˜์— ๋Œ€ํ•œ ์—ฐ์Šต์ด ๋ถ€์กฑํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
728x90