Language_Coder 570 : 배열2 - 자가진단7

boom.jun.cho·2022년 6월 3일
0

Language_Coder_JUNGOL

목록 보기
141/197

문제

배열을 만들어서 아래와 같이 저장한 후 출력하는 프로그램을 작성하시오.

규칙은 첫 번째 행은 모두 1로 초기화 하고 다음 행부터는 바로 위의 값과 바로 왼쪽의 값을 더한 것이다.

출력

코드

package com.jungol.algorithm141;

public class Main {
    public static void main(String[] args) {
        int[][] score = new int[5][5];

        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score[i].length; j++) {
                score[i][j]  = 1;
            }
        }

        for (int i = 1; i < score.length; i++) {
            for (int j = 1; j < score[i].length; j++) {
                score[i][j] = score[i][j - 1] + score[i - 1][j];
            }
        }

        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score[i].length; j++) {
                System.out.print(score[i][j] + " ");
            }
            System.out.println();
        }
    }
}
	
profile
하루하루 최선을

0개의 댓글