📝 문제
🔑 풀이 과정
예제 입력 N = 5 일때 출력을 보면
i번째 줄 | 공백 | * |
i = 1 | 0칸(i-1) | 9개(2N-(2i-1)) |
i = 2 | 1칸(i-1) | 7개(2N-(2i-1)) |
i = 3 | 2칸(i-1) | 5개(2N-(2i-1)) |
i = 4 | 3칸(i-1) | 3개(2N-(2i-1)) |
i = 5 | 4칸(i-1) | 1개(2N-(2i-1)) |
* 문제에서 첫째줄은 2N-1, 둘째줄은 2N-3.. 이랬으니깐
2N은 고정시키고 뒤에 1, 3...을 i로 표현하는 방법 찾기
* (2N-(2i-1))
은
2N-2i+1로 정리 가능
🔓 답안
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
for(int i = 1; i <= N; i++){
for(int j = 0; j < i-1; j++)
bw.write(" ");
for(int j = 0; j < (2*N)-(2*i)+1 ; j++)
bw.write("*");
bw.newLine();
}
bw.flush();
bw.close();
}
}
🖤 알고리즘 분류
- 구현
'PS > Baekjoon' 카테고리의 다른 글
[Baekjoon] 2178 - 미로 탐색 (0) | 2023.03.01 |
---|---|
[Baekjoon] 2444 - 별 찍기 - 7 (0) | 2023.02.28 |
[Baekjoon] 2442 - 별 찍기 - 5 (0) | 2023.02.26 |
[Baekjoon] 10798 - 세로읽기 (0) | 2023.02.25 |
[Baekjoon] 2563 - 색종이 (0) | 2023.02.24 |
댓글