지구정복

[DP] 백준 - BABBA 본문

데이터 엔지니어링 정복/Algorithm

[DP] 백준 - BABBA

nooh._.jl 2021. 7. 16. 15:54
728x90
반응형

-자바

package dp;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class BABBA {
	private static BufferedWriter bw = 
			new BufferedWriter( new OutputStreamWriter(System.out));
	private static int n;
	private static Tmp[] d; 

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt( br.readLine() );
		
		d = new Tmp[n+1];
		if( n <= 1 ) {
			d[1] = new Tmp( 0, 1 );
			
			bw.write( d[n].a + " " + d[n].b + "\n" );
			bw.flush();
			return;
		}
		d[1] = new Tmp( 0, 1 );
		d[2] = new Tmp( 1, 1 );
		
		for( int i=3; i<=n; i++ ) 
			d[i] = new Tmp( d[i-1].a+d[i-2].a, d[i-1].b+d[i-2].b );
		
		bw.write( d[n].a + " " + d[n].b + "\n" );
		bw.flush();
		
		bw.close();
		br.close();
	}
}

class Tmp {
	int a, b;

	public Tmp(int a, int b) {
		super();
		this.a = a;
		this.b = b;
	}
}

 

-파이썬

k = int(input())

d = [ [0]*2 for i in range( k+1 ) ]

if k <= 1:
    d[1][0] = 0
    d[1][1] = 1
    print( d[1][0], d[1][1] )
else:
    d[1][0] = 0
    d[1][1] = 1
    d[2][0] = 1
    d[2][1] = 1
    for i in range( 3, k+1 ):
        d[i][0] = d[i-1][0]+d[i-2][0]
        d[i][1] = d[i-1][1]+d[i-2][1]

    print( d[k][0], d[k][1] )
728x90
반응형
Comments