File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.math.BigInteger ;
4+ import java.util.* ;
5+
6+ class Main {
7+
8+ private static int N ;
9+ private static StringBuilder sb;
10+
11+ public static void main (String [] args ) throws IOException {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
13+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
14+
15+ N = Integer . parseInt(br. readLine());
16+ sb = new StringBuilder ();
17+
18+ BigInteger bigTwo = new BigInteger (" 2" );
19+
20+ // bw.flush()를 생략하면 틀리는 이유?
21+ // N=20일때, 아래 스트링빌더에 쓴 것도 같이출력해줘야 해서 bad -> flush를 해주면 통과하는구나
22+ bw. write(bigTwo. pow(N ). subtract(BigInteger . ONE ) + " \n " );
23+ bw. flush();
24+ // System.out.println(bigTwo.pow(N).subtract(BigInteger.ONE));
25+ if (N <= 20 ) {
26+ hanoi(N , 1 , 3 );
27+ bw. write(sb + " " );
28+ }
29+
30+ br. close();
31+ bw. flush();
32+ bw. close();
33+ }
34+
35+ private static void hanoi (int disks , int start , int end ) {
36+ if (disks == 1 ) {
37+ sb. append(start + " " + end + " \n " );
38+ return ;
39+ }
40+ int via = 6 - start - end; // 경유지
41+ hanoi(disks- 1 , start, via);
42+ hanoi(1 , start, end);
43+ hanoi(disks- 1 , via, end);
44+ }
45+ }
46+
47+ ```
48+
49+ - BigInteger
50+ - BufferedWriter 사용 시 주의
You can’t perform that action at this time.
0 commit comments