개발자's Life

[Coding Test] 백준 - 별 찍기 2 본문

코딩테스트

[Coding Test] 백준 - 별 찍기 2

Rowen Jobs 2023. 3. 12. 19:45
728x90
반응형

언어 - java11 

문제

첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제

하지만, 오른쪽을 기준으로 정렬한 별(예제 참고)을 출력하시오.

입력

 

첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다.

출력

첫째 줄부터 N번째 줄까지 차례대로 별을 출력한다.

우선 빈칸 찍어주는 반복문과 별을 찍어주는 반복문으로 작업을 진행 하였습니다. 

import java.util.Scanner;

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

        int total_cnt = sc.nextInt();
        // 빈칸을 찍어주기 위해 변수에 담아줍니다.
        int empty_cnt = total_cnt;
        
        
        for(int i = 0; i < total_cnt; i++){
            empty_cnt--;
            
            // 빈칸 찍기
              for(int j = 0; j < empty_cnt; j++){
                System.out.print(" ");
            }
			
            // 별 찍기
            for(int z = 0; z <= i; z++){
                System.out.print("*");
            }
			
            // 마지막 반복문이 아니라면 줄바꿈 실행
            if(i + 1 != total_cnt){
                System.out.println();
            }
        }
    }
}
728x90
Comments