1. #include <stdio.h>
    2. void hanoi(int a, int b, int c, int d){
    3. if (a == 1){
    4. printf("move %d b %d d %d\n", a, b, d );
    5. return ;
    6. }
    7. hanoi(a - 1, b, d, c);
    8. printf("move %d b %d c %d\n", a, b, c);
    9. hanoi(a - 1, d, b, c);
    10. }
    11. int main(){
    12. hanoi(3, 1, 2, 3);
    13. return 0;
    14. }

    a654638d2aa14bdb9d21e332419be4c.png
    以上为学习过程
    以下为老师教

    #include <stdio.h>
    
    void hanoi(int n, int from, int mid, int to) {
        if (n == 1) {
            printf("Move %d from %d to %d\n", n, from, to);
    
            return;
        }
    
        hanoi(n - 1, from, to, mid);
        printf("Move %d from %d to %d\n", n, from, to);
        hanoi(n - 1, mid, from, to);
    }
    
    int main() {
        hanoi(3, 1, 2, 3);
    
        return 0;
    }
    

    3920b5034753cdc56881a2ade7775f0.png