반응형
Notice
Recent Posts
Recent Comments
Link
지구정복
[DP] 백준 - BABBA 본문
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
반응형
'데이터 엔지니어링 정복 > Algorithm' 카테고리의 다른 글
[DP] 백준 - 파스칼의 삼각형 (0) | 2021.07.16 |
---|---|
[DP] 백준 - 타일 장식물 (0) | 2021.07.16 |
[DP] 백준 - 퇴사2 (0) | 2021.07.16 |
[DP] 백준 - 설탕배달 (0) | 2021.07.15 |
[문자열] 백준 - Pen Pineapple Apple Pen (0) | 2021.07.15 |
Comments