Algorithm/JUNGOL

[정올] 148 : 반복제어문3 - 형성평가9

Gyuri 2022. 1. 6. 01:36

문제

자연수 n을 입력받아 "출력 예"와 같이 공백으로 구분하여 출력되는 프로그램을 작성하시오.

주의! '#'은 공백으로 구분하되 줄사이에 빈줄은 없다.

 

입력 예

3

출력 예

#
# #
# # #
  # #
    #

 

코드

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        for(int i=0; i<n; i++) {
            for(int j=1; j<=i+1; j++) {
                System.out.print("# ");
            }
            System.out.println();
        }

        for(int i=0; i<n-1; i++) {
            for(int j=1; j<=i+1; j++) {
                System.out.print("  ");
            }

            for (int j = 1; j <= n - i - 1; j++) {
                System.out.print("# ");
            }
            System.out.println();
        }
    }
}