반응형

#include <stdio.h>

 

int number,cnt;

void movetower(int a, int b, int c, int d);
void movedisk(int a, int b, int c);

 

int main(void)

{
   printf("Insert the number of disks of hanoi tower!\n");
   scanf("%d", &number);
   movetower(number, 1, 2, 3);
   printf("The number of move is %d.\n",cnt);
   return 0;
}

 

void movetower(int a, int b, int c, int d)

{
   if(a==1) {
      movedisk(a,b,d);
   }
   else {
      movetower(a-1, b, d, c);
      movedisk(a, b, d);
      movetower(a-1, c, b, d);
   }
}

 

void movedisk(int a, int b, int c)

{
   printf("Move disk %d from %d to %d.\n",a,b,c);
   cnt++;
}

 
반응형

+ Recent posts