반응형
문제 출처 :
https://www.acmicpc.net/problem/10826
알고리즘 분석 :
문제 해결에 필요한 사항
1. BigInteger Library :: http://www.crocus.co.kr/622
java의 biginteger 라이브러리를 쓰면 쉽게 풀린다.
피보나치의 규칙을 이용하여 변수 3개로 해결 할 수 있다.
소스 코드 :
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 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger a = new BigInteger("1"); BigInteger b = new BigInteger("1"); BigInteger c = new BigInteger("2"); Scanner sc = new Scanner(System.in); int n; n = sc.nextInt(); if(n == 0) System.out.println(0); else if(n == 1) System.out.println(1); else if(n == 2) System.out.println(1); else { for(int i = 3; i <= n; i ++) { c = b.add(a); a = b; b = c; } System.out.println(c); } } } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[11444번] 피보나치 수 6 (0) | 2017.04.15 |
---|---|
[2749번] 피보나치 수 3 (0) | 2017.04.15 |
[9177번] 단어 섞기 (0) | 2017.04.15 |
[1850번] 최대공약수 (0) | 2017.04.14 |
[1252번] 이진수 덧셈 (0) | 2017.04.14 |