C/C 개발

C언어 하노이의 탑 코드

CE : 하랑 2021. 12. 25. 17:33

 

결과물

 

 

 


 

코드

 

 

 

 


 

복사코드

 

#include <stdio.h>

void hanoi_tower(int n, char from, char tmp, char to)  //하노이의 탑
{
if (n == 1)
printf("원판 1을 %c 에서 %c으로 옮긴다.\n", from, to);
else 
{
hanoi_tower(n - 1, from, to, tmp);
printf("원판 %d을 %c에서 %c으로 옮긴다.\n", n, from, to);
hanoi_tower(n - 1, tmp, from, to);
}
}

int main()
{
int num;
printf("하노이의 탑 층 입력 : ");
scanf("%d", &num);
hanoi_tower(num, 'A', 'B', 'C');
return 0;
}