재귀함수 하노이탑
페이지 정보
작성일 19-10-20 15:15본문
Download : 재귀함수 하노이탑.hwp
…(투비컨티뉴드 )
순서
설명
재귀함수 하노이탑






다.
재귀함수 하노이탑
재귀함수 하노이탑 , 재귀함수 하노이탑기타레포트 , 재귀함수 하노이탑
레포트/기타
Download : 재귀함수 하노이탑.hwp( 44 )
재귀함수,하노이탑,기타,레포트
재귀함수 하노이탑
(n〓5일 때 결과출력 및 분석)
1. 하노이탑 코드
#include `stdio.h`
void hanoi_tower(int n, char from, char tmp, char to)
{
if(n1)
printf(`원판 1을 %c에서 %c로 옮긴다. ₩n`, from, to);
else
{
hanoi_tower(n-1, from, to, tmp);
printf(`원판 %d을 %c에서 %c로 옮긴다. ₩n`, from, to);
else { //3 2번의 과정이 실행되지 않았으므로, 이것이 실행됩니다.
printf(`원판 1을(를) %c에서 %c로 옮긴다. ₩n`, n, from, to);
hanoi_tower(n-1, tmp, from, to);
}
}
int main(void)
{
hanoi_tower(5, `A`, `B`, `C`);
return 0;
}
2. 출력결과
3. n〓5일 때 분석
int main(void)
{
hanoi_tower(5, `A`, `B`, `C`);
return 0;
}
첫 번째 하노이탑 함수가 실행합니다.
hanoi_tower(n-1, from, to, tmp); //4 자신의 함수를 다시 호출합니다.
void hanoi_tower(int n, char from, char tmp, char to)
void hanoi_tower(5, ‘A’, ’B’, ’C’);를 호출합니다. //1 n은 5, 변수 from tmp to는 문자 A B C를 갖는다
{
if(n〓1) //2 n이 1의 값을 가질 때. 지금은 5의 값이므로 실행되지 않습니다. 단, 보내는 값은 1이 줄여줍니다.