https://school.programmers.co.kr/learn/courses/30/lessons/12945
๋ฌธ์ ์ค๋ช
ํผ๋ณด๋์น ์๋ F(0) = 0, F(1) = 1์ผ ๋, 1 ์ด์์ n์ ๋ํ์ฌ F(n) = F(n-1) + F(n-2) ๊ฐ ์ ์ฉ๋๋ ์ ์ ๋๋ค.
์๋ฅผ๋ค์ด
- F(2) = F(0) + F(1) = 0 + 1 = 1
- F(3) = F(1) + F(2) = 1 + 1 = 2
- F(4) = F(2) + F(3) = 1 + 2 = 3
- F(5) = F(3) + F(4) = 2 + 3 = 5
์ ๊ฐ์ด ์ด์ด์ง๋๋ค.
2 ์ด์์ n์ด ์ ๋ ฅ๋์์ ๋, n๋ฒ์งธ ํผ๋ณด๋์น ์๋ฅผ 1234567์ผ๋ก ๋๋ ๋๋จธ์ง๋ฅผ ๋ฆฌํดํ๋ ํจ์, solution์ ์์ฑํด ์ฃผ์ธ์.
์ ํ ์ฌํญ
- n์ 2 ์ด์ 100,000 ์ดํ์ธ ์์ฐ์์ ๋๋ค.
์ ์ถ๋ ฅ ์
n | return |
5 | 5 |
3 | 2 |
์ ์ถ๋ ฅ ์ ์ค๋ช
ํผ๋ณด๋์น์๋ 0๋ฒ์งธ๋ถํฐ 0, 1, 1, 2, 3, 5, ... ์ ๊ฐ์ด ์ด์ด์ง๋๋ค.
๋ด ํด๋ต
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int n) {
int fibo[100001] = {0,};
fibo[1] = 1;
for(int i = 2; i <= n; ++i)
{
fibo[i] = fibo[i-1] + fibo[i-2];
fibo[i] %= 1234567;
}
return fibo[n];
}
์ฌ๊ท๋ก ํ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋ ์ ๋ฐฐ์ด๋ก ํ์๋ค.
'๐ฅ๏ธ Study Note > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค / level.2 / ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ(C++) (0) | 2023.05.06 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค / level.2 / ๋ค์ ํฐ ์ซ์(C++) (0) | 2023.05.01 |
[๋ฐฑ์ค] ์ค๋ชฉ(C++) (0) | 2023.04.24 |
ํ๋ก๊ทธ๋๋จธ์ค / level.1 / ์์ฐ์ ๋ค์ง์ด ๋ฐฐ์ด๋ก ๋ง๋ค๊ธฐ(C++) (1) | 2023.04.23 |
ํ๋ก๊ทธ๋๋จธ์ค / level.3 / ์ฌ ์ฐ๊ฒฐํ๊ธฐ(C++) (0) | 2023.04.21 |