문제 출처: https://www.acmicpc.net/problem/2447
문제
재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다.
크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이 하나씩 있는 패턴이다.
***
* *
***
N이 3보다 클 경우, 크기 N의 패턴은 공백으로 채워진 가운데의 (N/3)×(N/3) 정사각형을 크기 N/3의 패턴으로 둘러싼 형태이다. 예를 들어 크기 27의 패턴은 예제 출력 1과 같다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
private static char[][] star;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
StringBuilder sb = new StringBuilder();
star = new char[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
star[i][j] = '*';
}
}
recursive(0, 0, N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sb.append(star[i][j]);
}
sb.append("\n");
}
System.out.println(sb);
}
private static void recursive(int startRow, int startCol, int N) {
if (N == 1) return;
for (int i = startRow + N / 3; i < startRow + N / 3 * 2; i++) {
for (int j = startCol + N / 3; j < startCol + N / 3 * 2; j++) {
star[i][j] = ' ';
}
}
recursive(startRow, startCol, N / 3);
recursive(startRow, startCol + N / 3, N / 3);
recursive(startRow, startCol + N / 3 * 2, N / 3);
recursive(startRow + N / 3, startCol, N / 3);
recursive(startRow + N / 3, startCol + N / 3 * 2, N / 3);
recursive(startRow + N / 3 * 2, startCol, N / 3);
recursive(startRow + N / 3 * 2, startCol + N / 3, N / 3);
recursive(startRow + N / 3 * 2, startCol + N / 3 * 2, N / 3);
}
}