본문 바로가기
Problem Solving/Baekjoon

[백준] 26948 Plankan - Dynamic Programming / Java

by graycode 2024. 2. 18.

 문제 링크

 

26948번: Plankan

Man vill skapa en längre planka med hjälp av ett antal mindre brädor. Det finns tre olika typer av brädor, som har längden $1$, $2$ respektive $3$ meter. Det finns ett obegränsat antal av varje typ. Det finns $7$ sätt att limma ihop en planka som ä

www.acmicpc.net

 

 풀이 코드

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read();
        int[] cache = new int[n];

        cache[0] = 1;
        cache[1] = 2;
        cache[2] = 4;
        for (int i = 3; i < n; i++) cache[i] = cache[i - 1] + cache[i - 2] + cache[i - 3];

        bw.write(String.valueOf(cache[n - 1]));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글