백준 - 2748 피보나치 수 2

AekT·2021년 11월 8일
post-thumbnail

백준 2748 피보나치 수 2

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

Swift :

var arr = Array(repeating: 0, count: 91)
arr[1] = 1
arr[2] = 1
guard let x = Int(readLine()!) else { fatalError() }
print(fibo(x))


func fibo(_ x: Int) -> Int{
    if x < 3 {
        return 1
    }
    else if arr[x] != 0 {
        return arr[x]
    }
    arr[x] = fibo(x-1) + fibo(x-2)
    return arr[x]
}
profile
으악

0개의 댓글