Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2193๋ฒˆ - ์ด์นœ์ˆ˜ (์Šค์œ„ํ”„ํŠธ dp, ๋™์ ๊ณ„ํš๋ฒ•, 2์ฐจ์›)

๊ฐ์ž ๐Ÿฅ” 2022. 3. 12. 16:42
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ

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

 

2193๋ฒˆ: ์ด์นœ์ˆ˜

0๊ณผ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์ง„์ˆ˜ ์ค‘ ํŠน๋ณ„ํ•œ ์„ฑ์งˆ์„ ๊ฐ–๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋Š”๋ฐ, ์ด๋“ค์„ ์ด์นœ์ˆ˜(pinary number)๋ผ ํ•œ๋‹ค. ์ด์นœ์ˆ˜๋Š” ๋‹ค์Œ์˜ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•œ๋‹ค. ์ด์นœ์ˆ˜๋Š” 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š

www.acmicpc.net

 

ํ’€์ด IDEA

N=2 ๋ถ€ํ„ฐ ๊ทœ์น™์„ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ, ์ด์ „์— ํ’€์—ˆ๋˜ ๋™์ ๊ณ„ํš๋ฒ• ๋ฌธ์ œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๋์ž๋ฆฌ์— ๋”ฐ๋ผ์„œ ๊ฒฝ์šฐ๊ฐ€ ๋‚˜๋‰˜์—ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ ํ™”์‹์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 

let n = Int(readLine()!)!
var dp = [[Int]](repeating: Array(repeating: 0, count: 2), count: n+1)

if n == 1 {
    print(1)
} else {
    dp[2][0] = 1
    dp[2][1] = 0

    for i in 3..<n+1 {
        dp[i][0] = dp[i-1][0] + dp[i-1][1]
        dp[i][1] = dp[i-1][0]
    }

    print(dp[n][0]+dp[n][1])
}
๋ฐ˜์‘ํ˜•