From 666870f65925942165dceaf43bbadd25ca020e08 Mon Sep 17 00:00:00 2001 From: Donew <47556347+03do-new30@users.noreply.github.com> Date: Mon, 10 Feb 2025 16:14:23 +0900 Subject: [PATCH] =?UTF-8?q?[20250210]=20BOJ=20/=20=EA=B3=A8=EB=93=9C5=20/?= =?UTF-8?q?=20=ED=95=98=EB=85=B8=EC=9D=B4=20=ED=83=91=20/=20=EC=8B=A0?= =?UTF-8?q?=EB=8F=99=EC=9C=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\353\205\270\354\235\264 \355\203\221.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "03do-new30/202502/10 BOJ G5 \355\225\230\353\205\270\354\235\264 \355\203\221.md" diff --git "a/03do-new30/202502/10 BOJ G5 \355\225\230\353\205\270\354\235\264 \355\203\221.md" "b/03do-new30/202502/10 BOJ G5 \355\225\230\353\205\270\354\235\264 \355\203\221.md" new file mode 100644 index 00000000..9d220e9d --- /dev/null +++ "b/03do-new30/202502/10 BOJ G5 \355\225\230\353\205\270\354\235\264 \355\203\221.md" @@ -0,0 +1,50 @@ +```java +import java.io.*; +import java.math.BigInteger; +import java.util.*; + +class Main { + + private static int N; + private static StringBuilder sb; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + N = Integer.parseInt(br.readLine()); + sb = new StringBuilder(); + + BigInteger bigTwo = new BigInteger("2"); + + // bw.flush()를 생략하면 틀리는 이유? + // N=20일때, 아래 스트링빌더에 쓴 것도 같이출력해줘야 해서 bad -> flush를 해주면 통과하는구나 + bw.write(bigTwo.pow(N).subtract(BigInteger.ONE) + "\n"); + bw.flush(); +// System.out.println(bigTwo.pow(N).subtract(BigInteger.ONE)); + if (N <= 20) { + hanoi(N, 1, 3); + bw.write(sb + ""); + } + + br.close(); + bw.flush(); + bw.close(); + } + + private static void hanoi(int disks, int start, int end) { + if (disks == 1) { + sb.append(start + " " + end + "\n"); + return; + } + int via = 6 - start - end; // 경유지 + hanoi(disks-1, start, via); + hanoi(1, start, end); + hanoi(disks-1, via, end); + } +} + +``` + +- BigInteger +- BufferedWriter 사용 시 주의