Explanation Hanoi Code (Recursion) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Explanation Hanoi Code (Recursion)

Hello, Can you explain how the following code works? Especially, what does n-1 and how do you modify the variables? package Semana2_Recursion; public class Hanoi { public static void main(String[] args) { solucionar('A','B','C', 2); // Llamado inicial con 2 discos } public static void solucionar(char A, char B, char C, int n) { if (n>0) { solucionar(A,C,B,n-1); System.out.println("Movimiento "+A+"->"+C); solucionar(B,A,C, n-1); } } }

16th Oct 2018, 8:53 PM
Jorge Forero P
Jorge Forero P - avatar
1 Answer
+ 1
just tace this with pen and paper for 3 disk you will get the idea for n disks.
16th Oct 2018, 10:07 PM
Tanay
Tanay - avatar