题解 | 汉诺塔

汉诺塔

https://www.nowcoder.com/practice/dd0dc9588fd94127b594b2adc3ebb346

#include <iostream>
using namespace std;
void hanoi(int ,char start,char end,char temp);

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

void hanoi(int n,char start,char end,char temp){
    if(n==1){
        cout<<start<<" "<<end<<endl;
	  	return;//结束函数,不再执行后面的代码
    }
  	//递归部分
    hanoi(n-1,start,temp,end);
    cout<<start<<" "<<end<<endl;
    hanoi(n-1,temp,end,start);
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务