[백준] 2439 : 별 찍기 - 2 - Java

길 잃은 까마귀·2022년 9월 14일
0

https://www.acmicpc.net/problem/2439


  • 문제

  • 풀이
    이중 for문을 이용한 문제인데 별 찍기 1과 다른점은 공백을 먼저 출력하고 그 다음에 별을 출력하는 것이다. 공백을 N-i개 만큼 먼저 출력한후 별찍기 1과 같이 별을 하나부터 출력해주면 된다.

  • 코드
import java.util.*;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int i, t, j;
		for (i = 1; i <= N; i++) {
			for (t = 0; t < N - i; t++)
				System.out.printf(" ");
			for (j = 0; j < i; j++) {
				System.out.printf("*");
			}
			System.out.printf("\n");
		}
		sc.close();
	}
}
profile
코딩 고수가 될 사람

0개의 댓글