Question 251.2022 Winter Ladder Training - 7-14 Tower of Hanoi Problem (10 points)


Question 251.2022 Winter Ladder Training - 7-14 Tower of Hanoi Problem (10 points)


1. The topic

insert image description here
insert image description here

2. Problem solving

#include <bits/stdc++.h>

using namespace std;

void move(char src,char des)
{
    
    
    printf("%c-->%c\n",src,des);
}

void hanoi(int n,char src,char med,char des)
{
    
    
    if(n==1)
    {
    
    
        move(src,des);
        return;
    }
    hanoi(n-1,src,des,med);
    move(src,des);
    hanoi(n-1,med,src,des);
}

int main()
{
    
    
    int n;
    cin>>n;
    hanoi(n,'A','B','C');
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324346271&siteId=291194637