less than 1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅ‰ Bronze 1


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/2748


image


๐Ÿ“Œ ํ’€์ด

๋ฌธ์ œ ์ž์ฒด๋Š” ์ •๋ง ๊ฐ„๋‹จํ•˜๋‹ค.
์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด๋„ ๋˜๋Š” ๋ฌธ์ œ์ด์ง€๋งŒ, ์ด ๋ฌธ์ œ๋Š” ํ”ผ๋ณด๋‚˜์น˜2๋ผ ์‹œ๊ฐ„์ œํ•œ์ด 1์ดˆ๋ฐ–์— ์•ˆ๋˜์„œ ์žฌ๊ท€๊ฐ™์€ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋†’์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€๋ฉด ์ œํ•œ์‹œ๊ฐ„์— ๊ฑธ๋ฆฌ๊ฒŒ๋œ๋‹ค.
๋”ฐ๋ผ์„œ DP์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜์—ฌ์•ผ ์ œํ•œ์‹œ๊ฐ„๋‚ด์— ํ’€ ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด Bottom-Up ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.


๐Ÿ“Œ Code

package DP;

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

public class N2748 {
    public static long[] fibo;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int count = Integer.parseInt(st.nextToken());
        fibo = new long[count+1];
        Arrays.fill(fibo, -1);

        fibo[0] = 0;
        if(count != 0) {
            fibo[1] = 1;
            for (int i = 2; i < count + 1; i++) {
                MakeFibo(i);
            }
        }
        System.out.println(fibo[count]);

    }
    public static void MakeFibo(int i){
        if(fibo[i]==-1) {
            fibo[i] = fibo[i - 1] + fibo[i - 2];
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜