#include <stdio.h>void main () { extern a; extern b; Extern c; extern s; void hanno (Int,char,char,char); void Setnum (int); int n; printf ("please input the number of dishes: "); &NBSP;&NBSP;SCANF ("%d ", &n); printf (" The following is moving %d dishes ...\n ( %2d 0 0) \ n ", n,n); setnum (n); hanno (n, ' A ', ' B ', ' C ');} Int a;int b;int c;int s;void setnum (int m) { a=m;} Void hanno (INT&NBSP;M,CHAR&NBSP;PILLAR1,CHAR&NBSP;PILLAR2,CHAR&NBSP;PILLAR3) { void move ( Char,char,int); if (m==1) { s++; move (PILLAR1, Pillar3,s); } else { &nbsP; hanno (M-1,PILLAR1,PILLAR3,PILLAR2); s++; move (PILLAR1, Pillar3,s); hanno (M-1,PILLAR2,PILLAR1,PILLAR3); };} Void move (char x,char y,int s) { if (x== ' A ' && a>0 ) a--; else if (x== ' B ' && b>0 ) b--; else if (x== ' C ' && c>0 ) c--; if (y = = ' A ') a++; else if (y== ' B ') b++; else if (y== ' C ') c+ +; printf ("%3d: %c --> %c (%2d %2d %2d) \ n", s,x,y,a,b,c);}
#gcc Hannuota.c-o Hannuota
#./hannuota
Please input the number of Dishes:3
The following is moving 3 dishes ...
(3 0 0)
1: A--C (2 0 1)
2: A-to B (1 1 1)
3:c-B (1 2 0)
4:a-C (0 2 1)
5:b-A (1 1 1)
6:b-C (1 0 2)
7:a-C (0 0 3)
Hanoi algorithm Learning-C code