코딩테스트

SWEA 2005 : 파스칼의 삼각형

joonwoong 2024. 2. 23. 16:41

문제.

 

풀이.

배열을 이용하여 칸을 채워가는 것으로 했음

 

코드.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
    static int N;
    static int[][] map;
    public static void main(String[] args) throws Exception {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int T=Integer.parseInt(st.nextToken());
        for (int tc = 1; tc <=T ; tc++) {
            st = new StringTokenizer(br.readLine());
            N=Integer.parseInt(st.nextToken());
            map=new int[N][N];
            for (int i = 0; i < N; i++) {
                map[i][0]=1;
                map[i][i]=1;
                for (int j = 1; j < i; j++) {
                    map[i][j]=map[i-1][j]+map[i-1][j-1];
                }
            }
            System.out.println("#"+tc);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j <=i; j++) {
                    System.out.print(map[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
}

 

결과.

 

소감.

꾸준히 나오던 유형이라 쉬움.

'코딩테스트' 카테고리의 다른 글

SWEA 1215 : 회문1  (0) 2024.03.05
SWEA 1213 : String  (0) 2024.03.05
SWEA 2072 : 간단한 369게임  (0) 2024.02.22
SWEA 1859 : 백만장자 프로젝트  (0) 2024.02.22
백준 2751 : 수 정렬하기 2  (0) 2024.02.21