[백준 브론즈 IV] 2439번: 별 찍기 - 2

DONI·2021년 8월 7일
0

Baekjoon Online Judge

목록 보기
27/31
post-thumbnail

문제

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

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


입력

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


출력

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


예제 입력 1

5

예제 출력 1


소스코드

  • Java 첫 번째 방법 : for 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		String str = "";		
		for (int i = 0; i < n; i++) {
			for (int j = n - 1; j > i; j--)
				System.out.print(" ");
			str += "*";
			System.out.println(str);
		}
	}
}
  • Java 두 번째 방법 : while 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		String str = "";		
		int i = 0;
		while (i < n) {
			int j = n - 1;
			while (j > i) {
				System.out.print(" ");
				j--;
			}
			str += "*";
			System.out.println(str);
			i++;
		}
	}
}
  • Java 세 번째 방법 : do-while 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		String str = "";
		int i = 0;
		do {
			int j = n - 1;
			while (j > i) {
				System.out.print(" ");
				j--;
			}
			str += "*";
			System.out.println(str);
			i++;
		} while (i < n);
	}
}

[바로가기] 2439번: 별 찍기 - 2

profile
틀린 내용이 있다면 댓글 또는 이메일로 알려주세요 ❤ꔛ❜

0개의 댓글