int fibonacci(int n)
It is a problem of overriding the Fibonacci function to find out how many times fibonacci(0) and fibonacci(1) were called.Before we solve this problem, I will describe the Fibonacci Sequence.
The Fibonacci sequence has the following rules.
fibonacci(0) = 1, fibonacci(1) = 1,
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
In the example, If 'n' is 3,
=> fibonacci(3) = fibonacci(3-2) + fibonacci(3-1)
=> fibonacci(3) = fibonacci(1) + fibonacci(2)
=> fibonacci(3) = fibonacci(1) + fibonacci(1) + fibonacci(0) = 3.
Because of these rules, we have to calculate the fibonacci sequence repeatedly.
It does not matter if the value is small, but in the opposite case
it is necessary to apply DP which is an algorithm to save the calculated value.
Let's solve it with DP.
<pesudo code>
*Source of the problem = https://www.acmicpc.net/problem/1003
*문제 출처 : BAEKJOON ONLINE JUDGE
댓글
댓글 쓰기