본문 바로가기
Problem Solving/Baekjoon

[백준] 7515 Prehistoric Operating Systems - Dynamic Programming / Java

by graycode 2024. 2. 19.

 문제 링크

 

7515번: Prehistoric Operating Systems

It is the year 2254. Ohio Smith is a specialist in dealing with ancient operating systems. For research purposes, he tries to install several operating systems on his computer which were used about 250 years ago. Prior research has yielded the result that

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));
        StringBuilder sb = new StringBuilder();

        int[] cache = new int[41];
        cache[0] = 1;
        cache[1] = 2;
        for (int i = 2; i < 41; i++) cache[i] = cache[i - 1] + cache[i - 2];

        int t = read();
        for (int i = 1; i <= t; i++)
            sb.append("Scenario ").append(i).append(":\n").append(cache[read()]).append("\n\n");

        bw.write(sb.toString());
        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;
    }

}

댓글