본문 바로가기
알고리즘-자료구조

[개탱][C언어][하노이탑][간단한 알고리즘][재귀호출][재귀함수][Stack][스택]

by 개탱 2017. 12. 21.
728x90

재귀호출을 사용한 하노이탑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <cmath>
int n;
void hanoi(int n, char from, char tmp, char to);
int iNum;
int main() {
    // '1', '2', '3'
    scanf("%d",&n);
    printf("%d\n",(int)pow(2,n)-1);
    hanoi(n,'1','2','3');
}
void hanoi(int n, char from, char tmp, char to)
{
    if(n==1)
    {
        printf("%c %c\n",from,to);
        iNum++;
        return;
    }
    hanoi(n-1,from,to,tmp);
    hanoi(1,from,tmp,to);
    hanoi(n-1,tmp,from,to);
}
cs


재귀호출을 사용하지 않은 방식


http://gaetaeng.tistory.com/11

댓글