반응형
Notice
Recent Posts
Recent Comments
Link
지구정복
[DP] 백준 - 파스칼의 삼각형 본문
728x90
반응형
-자바
package dp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class PascalTriangle {
private static BufferedWriter bw =
new BufferedWriter( new OutputStreamWriter(System.out));
private static int n, k;
private static int[][] d;
private static StringTokenizer st;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer( br.readLine() );
n = Integer.parseInt( st.nextToken() );
k = Integer.parseInt( st.nextToken() );
d = new int[n+1][];
for( int i=1; i<=n; i++ ) {
d[i] = new int[i+1];
for( int j=1; j<=i; j++ ) {
if( j==1 || j==i ) d[i][j] = 1;
else d[i][j] = d[i-1][j-1] + d[i-1][j];
}
}
bw.write( d[n][k] + "\n" );
bw.flush();
bw.close();
br.close();
}
}
-파이썬
n, k = map( int, input().split() )
d = [0] * (n+1)
for i in range(1,n+1):
d[i] = [0] * (i+1)
for j in range(1, i+1):
if j==1 or j==i:
d[i][j] = 1
else: d[i][j] = d[i-1][j-1]+d[i-1][j]
print( d[n][k] )
728x90
반응형
'데이터 엔지니어링 정복 > Algorithm' 카테고리의 다른 글
[수학] 백준 - 소수찾기 (0) | 2021.07.17 |
---|---|
[DP] 백준 - 다리놓기 (0) | 2021.07.17 |
[DP] 백준 - 타일 장식물 (0) | 2021.07.16 |
[DP] 백준 - BABBA (0) | 2021.07.16 |
[DP] 백준 - 퇴사2 (0) | 2021.07.16 |
Comments