Advertisement
SmnVadik

Tower of Hanoi (Java)

Aug 19th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         int numOfDiscs = 8;
  4.         solveHanoiTower(numOfDiscs, 'A', 'C', 'B');
  5.     }
  6.     public static void solveHanoiTower(int n, char source, char destination, char auxiliary){
  7.         if (n == 1) {
  8.             System.out.println("Переместить диск 1 из " + source + " в " + destination);
  9.             return;
  10.         }
  11.  
  12.         solveHanoiTower(n - 1, source, auxiliary, destination);
  13.         System.out.println("Переместить диск " + n + " из " + source + " в " + destination);
  14.         solveHanoiTower(n - 1, auxiliary, destination, source);
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement