I00014 汉若塔问题的C++程序
生活随笔
收集整理的這篇文章主要介紹了
I00014 汉若塔问题的C++程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼來自維基教科書:C++ Programming As A Set Of Problems。
程序如下:
#include <iostream>void hanoi(int depth, int from, int to, int alternate){if(depth == 0){return;}hanoi(depth-1, from, alternate, to);std::cout << "Move ring " << depth << " from pole " << from << " to pole " << to << "." << std::endl;hanoi(depth-1, alternate, to, from);}int main(int argc, char** argv){hanoi(4, 1, 2, 3);return 0;}
程序運行結果如下:
Move ring 1 from pole 1 to pole 3. Move ring 2 from pole 1 to pole 2. Move ring 1 from pole 3 to pole 2. Move ring 3 from pole 1 to pole 3. Move ring 1 from pole 2 to pole 1. Move ring 2 from pole 2 to pole 3. Move ring 1 from pole 1 to pole 3. Move ring 4 from pole 1 to pole 2. Move ring 1 from pole 3 to pole 2. Move ring 2 from pole 3 to pole 1. Move ring 1 from pole 2 to pole 1. Move ring 3 from pole 3 to pole 2. Move ring 1 from pole 1 to pole 3. Move ring 2 from pole 1 to pole 2. Move ring 1 from pole 3 to pole 2.
轉載于:https://www.cnblogs.com/tigerisland/p/7564850.html
總結
以上是生活随笔為你收集整理的I00014 汉若塔问题的C++程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 应用 之路 天气预报(五
- 下一篇: IOS开发 REST请求 ASIHTTP