Advertisement
exmkg

Untitled

Aug 23rd, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3.  
  4. public class Hanoi {
  5.     private static class Solution {
  6.         private void hanoi(int n, int from, int to) {
  7.             if (n == 0) return;
  8.             int aux = (1 + 2 + 3) - (from + to);
  9.             hanoi(n - 1, from, aux);
  10.             System.out.println(n + " " + from + " " + to);
  11.             hanoi(n - 1, aux, to);
  12.         }
  13.  
  14.         public void run(InputReader in, PrintWriter out) {
  15.             int n = in.nextInt();
  16.             hanoi(n, 1, 3);
  17.         }
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         InputStream inputStream = System.in;
  22.         OutputStream outputStream = System.out;
  23.         InputReader in = new InputReader(inputStream);
  24.         PrintWriter out = new PrintWriter(outputStream);
  25.         Solution solution = new Solution();
  26.         solution.run(in, out);
  27.         out.close();
  28.     }
  29.  
  30.     private static class InputReader {
  31.         public BufferedReader reader;
  32.         public StringTokenizer tokenizer;
  33.  
  34.         public InputReader(InputStream stream) {
  35.             reader = new BufferedReader(new InputStreamReader(stream), 32768);
  36.             tokenizer = null;
  37.         }
  38.  
  39.         public String next() {
  40.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  41.                 try {
  42.                     tokenizer = new StringTokenizer(reader.readLine());
  43.                 } catch (IOException e) {
  44.                     throw new RuntimeException(e);
  45.                 }
  46.             }
  47.             return tokenizer.nextToken();
  48.         }
  49.  
  50.         public int nextInt() {
  51.             return Integer.parseInt(next());
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement