C
C, 하노이탑 소스
수알치
2016. 8. 11. 18:45
#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++;
}
반응형