하노이의탑
title: 2026-02-13 author: 강병호 (이름) date: 2026-02-13 (날짜) category: TIL/강병호/2026/02 (파일 경로 : TIL/{이름}/{연}/{월}) layout: post (자유) —
import java.util.*;
import java.io.*;
public class Q4779 {
static char[] charList;
static void recur(int start, int end, int N) {
if (N == 0) return;
// 1. 3등분
int mid1 = start + N/3;
int mid2 = mid1 + N/3;
for (int i = mid1; i < mid2; i++) {
charList[i] = ' ';
}
// System.out.println("start : " + start + " mid 1 : " + mid1 + " mid2 : " + mid2 + " end : " + end);
if (mid1 == mid2) return;
recur(start, mid1, N/3);
recur(mid2, end, N/3);
}
public static void main(String[] args) throws Exception {
// 가운데 문자열 공백 처리 방식 고민
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String st;
// EOF 확인
while ((st = br.readLine()) != null && !st.isEmpty()) {
int N = Integer.parseInt(st);
int length = (int) Math.pow(3, N);
charList = new char[length];
Arrays.fill(charList, '-');
recur(0, length, length);
// for (int i = 0; i < length; i++) {
// sb.append(charList[i]);
// }
sb.append(charList).append("\n");
}
System.out.print(sb);
}
}