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
|
/* Exercise 5.39 solution */
#include <stdio.h>
void tower( int, int, int, int );
int main()
{
int n;
printf( "Enter the starting number of disks: " );
scanf( "%d", &n );
tower( n, 1, 3, 2 );
return 0;
}
void tower( int c, int start, int end, int temp )
{
if ( c == 1 )
{
printf( "%d --> %d\n", start, end );
return;
}
/* move c - 1 disks from start to temp */
tower( c - 1, start, temp, end );
/* move last disk from start to end */
printf( "%d --> %d\n", start, end );
/* move c - 1 disks from temp to end */
tower( c - 1, temp, end, start );
}
|