forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.java
More file actions
26 lines (22 loc) ยท 845 Bytes
/
2.java
File metadata and controls
26 lines (22 loc) ยท 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
public class Main {
// ํ ๋ฒ ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ชจ์ด์ ์ด์
(Memoization)ํ๊ธฐ ์ํ ๋ฐฐ์ด ์ด๊ธฐํ
public static long[] d = new long[100];
// ํผ๋ณด๋์น ํจ์(Fibonacci Function)๋ฅผ ์ฌ๊ทํจ์๋ก ๊ตฌํ (ํ๋ค์ด ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ)
public static long fibo(int x) {
// ์ข
๋ฃ ์กฐ๊ฑด(1 ํน์ 2์ผ ๋ 1์ ๋ฐํ)
if (x == 1 || x == 2) {
return 1;
}
// ์ด๋ฏธ ๊ณ์ฐํ ์ ์๋ ๋ฌธ์ ๋ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
if (d[x] != 0) {
return d[x];
}
// ์์ง ๊ณ์ฐํ์ง ์์ ๋ฌธ์ ๋ผ๋ฉด ์ ํ์์ ๋ฐ๋ผ์ ํผ๋ณด๋์น ๊ฒฐ๊ณผ ๋ฐํ
d[x] = fibo(x - 1) + fibo(x - 2);
return d[x];
}
public static void main(String[] args) {
System.out.println(fibo(50));
}
}