zoukankan      html  css  js  c++  java
  • 递归-汉诺塔

    挺简单的代码吧,,,就是那个return ......

     1 #include <iostream>
     2 using namespace std;
     3 int n;
     4 void hanoi(int n,char src,char mid,char des){
     5     if(n==1){
     6         cout<<src<<"->"<<des<<endl;
     7         return ;//这个return 很有必要啊,递归边界.如果没有的话执行完if会继续执行hanoi n-1
     8     }
     9     hanoi(n-1,src,des,mid);
    10     cout<<src<<"->"<<des<<endl;
    11     hanoi(n-1,mid,src,des);
    12 
    13 
    14 }
    15 int main(){
    16     cin>>n;
    17     hanoi(n,'A','B','C');
    18 }
  • 相关阅读:
    Day 18
    Day 17
    Day 16
    Day 15
    Day 14
    Day 13
    Day 12
    Day 11
    Day 10
    《ES6标准入门》(阮一峰)--2.let 和 const 命令
  • 原文地址:https://www.cnblogs.com/zhmlzhml/p/13246313.html
Copyright © 2011-2022 走看看