SSONG Cloud
[SWEA] 2005 파스칼의 삼각형 본문
반응형
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
static void drawTriangle(int n) {
int[][] triangle = new int[n][n];
triangle[0][0] = 1;
for(int i = 1; i < n; i++) {
for(int j = 0; j < n; j++) {
if(j-1 >= 0) {
triangle[i][j] += triangle[i-1][j-1];
}
triangle[i][j] += triangle[i-1][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(triangle[i][j] == 0) {
System.out.print(" ");
continue;
}
System.out.print(triangle[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = Integer.parseInt(sc.nextLine());
for(int i = 1; i <= T; i++) {
int N = Integer.parseInt(sc.nextLine());
System.out.println("#"+i);
drawTriangle(N);
}
}
}
반응형
'Algorithm > SW Expert Academy' 카테고리의 다른 글
[SWEA] 2001 파리 퇴치 (0) | 2021.01.24 |
---|---|
[SWEA] 2805 농작물 수확하기 (0) | 2021.01.24 |
[SWEA] 1974 수도쿠 검증 (0) | 2021.01.24 |
[SWEA] 1284 수도 요금 경쟁 (0) | 2021.01.24 |
[SWEA] 간단한 369게임 (0) | 2021.01.24 |
Comments