본문 바로가기
Algorithm & SQL/BAEKJOON

백준 JAVA 2747 피보나치 수

by YoonJong 2022. 7. 3.
728x90

 

괜히 정답비율이 48%인게 아니였다..

피보나치로 구현하면 시간초과여서 일반 반복문으로 풀었어야 했다.

 

package BAEKJOON;

import java.util.Scanner;

public class NO2747 {

//    static int fibonacci(int n) {
//        if (n < 3) {
//            return 1;
//        }
//        return fibonacci(n - 2) + fibonacci(n - 1);
//    }

    public static void main(String[] args) {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        String s = br.readLine();
//        StringTokenizer st = new StringTokenizer(s);
//        int a = Integer.parseInt(st.nextToken());
//        System.out.println(fibonacci(a));

        Scanner sc = new Scanner(System.in);

//        int a = sc.nextInt();
//        System.out.println(fibonacci(a));

        int n = sc.nextInt();

        int a = 1;
        int b = 1;
        int c = 0;

        if (n == 1 || n == 2) {
            System.out.println(1);
        } else {
            for (int i = 1; i <= n; i++) {
                a = b;
                b = c;
                c = a + b;
            }
            System.out.println(c);
        }



    }
}
728x90

'Algorithm & SQL > BAEKJOON' 카테고리의 다른 글

백준 JAVA 1546 평균  (0) 2022.07.04
백준 JAVA 11719 그대로 출력하기 2  (0) 2022.07.04
백준 JAVA 2798 블랙잭  (0) 2022.06.30
백준 JAVA 5622 다이얼  (0) 2022.06.30
백준 JAVA 2292 벌집  (0) 2022.06.30

댓글