관리 메뉴

NineTwo meet you

[백준] 2446 별 찍기 - 9 본문

프로그래밍 문제/백준

[백준] 2446 별 찍기 - 9

NineTwo 2020. 6. 26. 14:25
반응형

출처


문제

예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.

입력

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

출력

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

예제


코드

import java.util.Scanner;
public class BOJ2446 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int blank = -1;
int star = 2*n+1;
//(j 개수만큼 빈칸 출력)(h 개수만큼 별 출력)(enter)
for(int i = 0; i < 2*n-1; i++) {
//빈칸은 1칸씩 늘어나고 별의 개수는 2개씩 줄어든다.
if(i < n) {
blank++;
star -= 2;
for(int j = 0; j < blank; j++) {
System.out.print(" ");
}
for(int j = 0; j < star; j++) {
System.out.print("*");
}
System.out.println();
}else { //빈칸은 1칸씩 줄어들고 별의 개수는 2개씩 늘어난다.
blank--;
star += 2;
for(int j = 0; j < blank; j++) {
System.out.print(" ");
}
for(int j = 0; j < star; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
view raw BOJ2446.java hosted with ❤ by GitHub

반응형

'프로그래밍 문제 > 백준' 카테고리의 다른 글

[백준] 2523 별 찍기 - 13  (0) 2020.06.26
[백준] 2522 별 찍기 - 12  (0) 2020.06.26
[백준] 2445 별 찍기 - 8  (0) 2020.06.26
[백준] 2444 별 찍기 - 7  (0) 2020.06.25
[백준] 2443 별 찍기 - 6  (0) 2020.06.25
Comments