CS Study/DataStructure
02 순환
mooh_0812
2022. 1. 18. 21:33
2.1 순환
어떤 알고리즘이나 함수가 자기 자신을 호출하여 문제를 해결하는 프로그래밍 기법
복귀주소가 시스템 스택에 저장되고 호출되는 함수를 위한 매개변수(parameter)와 지역변수를 스택으로 할당받음
#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, from, to, tmp);
}
}
int main(void)
{
hanoi_tower(4, 'A','B','C');
return 0;
}