알고리즘/그 외
백준 4150번: 피보나치 수 - Java
또또쪼
2020. 10. 2. 21:41
728x90
300x250
4150번: 피보나치 수
피보나치 수열은 다음과 같이 그 전 두 항의 합으로 계산되는 수열이다. 첫 두 항은 1로 정의된다. f(1) = 1, f(2) = 1, f(n > 2) = f(n − 1) + f(n − 2) 정수를 입력받아, 그에 해당하는 피보나치 수를 출력
www.acmicpc.net
BigInteger라는 자료형이 있다는 것을 처음 알았다.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class Main {
public static BigInteger[] dp;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
dp = new BigInteger[N+1];
dp[0]=BigInteger.valueOf(0);
dp[1]=BigInteger.valueOf(1);
if(N>1)
dp[2]=BigInteger.valueOf(1);
for (int i = 3; i <= N; i++) {
dp[i]=dp[i-1].add(dp[i-2]);
}
System.out.println(dp[N]);
}
}
728x90