1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include lt;stdio.hgt; #include lt;stdlib.hgt;
void towers(int, int, int, int);
int main() #123; char buffer[20]; int num;
printf("Enter the number of disks: "); fgets(buffer, sizeof(buffer), stdin); num = atoi(buffer); if (num lt; 1) #123; printf("Towers of Hanoi makes no sense with %d disks\n", num); return 1; #125;
printf("The moves to solve the Tower of Hanio are:\n"); towers(num, 1, 2, 3); return 0; #125;
* Recursive function that moves num pegs from * frompeg to topeg using auxpeg as an intermediate * storage location. */ void towers(int num, int frompeg, int topeg, int auxpeg) #123; if (num == 1) #123; printf("Move disk 1 from peg %d to peg %d\n", frompeg, topeg); return; #125; towers(num - 1, frompeg, auxpeg, topeg); printf("Move disk %d from peg %d to peg %d\n", num, frompeg, topeg); towers(num - 1, auxpeg, topeg, frompeg); #125;
|