Dev.baelanche

[백준 2193] 이친수 본문

Data Structure & Algorithm/PS - JAVA

[백준 2193] 이친수

baelanche 2019. 4. 2. 19:37
반응형

 

 

n자리 이친수의 개수를 세어보면 피보나치 수열이란 것을 알 수 있다.

 

 

 

1. n의 범위가 90 까지이므로 자료형을 long 으로 선언해야 한다.

 

public static long pinary(int n) {
        if(n == 0) {dp[0] = 0; return 0;}
        if(n == 1) {dp[1] = 1; return 1;}
        if(dp[n] != -1) return dp[n];
        
        long result = pinary(n-2) + pinary(n-1);
        dp[n] = result;
        return result;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        dp = new long[n+1];
        
        for(int i=0; i<dp.length; i++)
            dp[i] = -1;
        
        System.out.println(pinary(n));
        sc.close();
}
반응형

'Data Structure & Algorithm > PS - JAVA' 카테고리의 다른 글

[백준 11726] 2xn 타일링  (0) 2019.04.02
[백준 1904] 01타일  (0) 2019.04.02
[백준 15873] 공백 없는 A+B  (4) 2019.04.01
[백준 1032] 명령 프롬프트  (0) 2019.04.01
[백준 1026] 보물  (0) 2019.04.01
Comments