[Java] 백준 2193 이친수

Lee GaEun·2025년 2월 20일

[Java] 알고리즘

목록 보기
57/93

2193 이친수 문제 링크

문제


#1

import java.awt.*;
import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[][] result = new int[N][2];
        result[0] = new int[]{0, 1};

        for(int i=1; i<N; i++) {
            result[i][0] = result[i-1][0] + result[i-1][1];
            result[i][1] = result[i-1][0];
        }

        System.out.println(result[N-1][0] + result[N-1][1]);
    }
}


#2

  • 문제를 풀 때, 특히 DP 문제에서 경계값을 테스트 해보는 습관을 가져야 될 듯
  • 해당 문제도 이전 코드에서 90을 넣었을 경우 -1581614984가 결과로 나옴
  • int로 해결할 수 없는 문제라는 뜻
import java.awt.*;
import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        long[][] result = new long[N][2];
        result[0] = new long[]{0, 1};

        for(int i=1; i<N; i++) {
            result[i][0] = result[i-1][0] + result[i-1][1];
            result[i][1] = result[i-1][0];
        }

        System.out.println(result[N-1][0] + result[N-1][1]);
    }
}

  • 정답!
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글