coding_test/BAEKJOON

백준 11729번 C언어 풀이

CodeJin 2021. 10. 1. 16:28

https://www.acmicpc.net/problem/11729

 

11729번: 하노이 탑 이동 순서

세 개의 장대가 있고 첫 번째 장대에는 반경이 서로 다른 n개의 원판이 쌓여 있다. 각 원판은 반경이 큰 순서대로 쌓여있다. 이제 수도승들이 다음 규칙에 따라 첫 번째 장대에서 세 번째 장대로

www.acmicpc.net

수업시간에 풀었던 문제.

 

math.h의 pow함수는 부동소수점때문인지 오류가 나서 틀린다고 뜬다.

 

#include <stdio.h>

void hanoi (int num, int from, int temp, int to) {
    if (num == 1) printf("%d %d \n", from , to);
    else {
        hanoi(num - 1, from, to, temp);
        printf("%d %d\n", from, to);
        hanoi(num - 1, temp, from, to);
    }
}

int main () {
    int input;
    scanf("%d", &input);
    printf("%d \n", (1 << input) - 1);
    hanoi(input, 1, 2, 3);
    return 0;
}