피보나치 수열을 최대 10000번째 숫자까지 구하는 문제.
딱 봐도 long long 을 넘어간다.
그래서 BigInteger를 사용함(Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
import java.math.BigInteger; | |
/** | |
* https://www.acmicpc.net/problem/10826 | |
* BOJ 백준온라인져지 10826 피보나치 수 4 풀이 | |
*/ | |
class Main { | |
public static void main(String[] args) throws IOException{ | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | |
int N = Integer.parseInt(br.readLine()); | |
BigInteger fibonacci[] = new BigInteger[N + 3]; | |
fibonacci[0] = BigInteger.ZERO; | |
fibonacci[1] = BigInteger.ONE; | |
for(int i = 2; i <= N; i++){ | |
fibonacci[i] = fibonacci[i - 1].add(fibonacci[i - 2]); | |
} | |
bw.write(fibonacci[N] + ""); | |
bw.flush(); | |
} | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 13241 최소공배수 풀이 (0) | 2017.12.08 |
---|---|
BOJ 백준온라인져지 1977 완전제곱수 풀이 (0) | 2017.12.08 |
BOJ 백준온라인져지 10757 큰 수 A+B 풀이 (0) | 2017.12.08 |
BOJ 백준온라인져지 2749 피보나치 수 3 풀이 (0) | 2017.12.08 |
BOJ 백준온라인져지 2748 피보나치 수 2 풀이 BOJ 백준온라인져지 10870 피보나치 수 5 풀이 (0) | 2017.12.08 |