当前位置:网站首页>汉诺塔问题的递归实现
汉诺塔问题的递归实现
2022-07-21 15:11:00 【小飞将】
// TowersOfHanoi.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <chrono>
void towersOfHanoi(int n, int t1, int t2, int t3) {
//将t1上所有盘移到t2
if (n > 0) {
towersOfHanoi(n - 1, t1, t3, t2); //为了将t1上最大的盘移到t2,必须将t1上n-1个盘,移到t3
std::cout << "Move top disk from tower " << t1 << " to top of tower " << t2 << std::endl;
towersOfHanoi(n - 1, t3, t2, t1); //将t3上n-1个盘,通过t1移至t2上
}
return;
}
int main()
{
auto start = std::chrono::steady_clock::now();
std::cout << "Enter the disk number of tower 1" << std::endl;
int diskNum;
std::cin >> diskNum;
towersOfHanoi(diskNum, 1, 2, 3);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
}
边栏推荐
猜你喜欢
win10下载地址
Lodash中数组和对象合并方法assign、merge、defaults、defaultsDeep比较
Mod17a3hgf data download
Foundation of Mathematics: Jensen inequality
Custom class loader implementation
非参数检验
QT - graphic view
ENVI shp转roi并对栅格进行掩膜提取
运行时,物体移动旋转缩放插件,“RuntimeTransformGizmos插件”使用教程(Unity3D)
Matlab GUI编程技巧(七):matlablistbox操作-列表框(ListBox)和uilistbox常用操作
随机推荐
EF linq杂记
MOD17A3HGF数据下载
敬伟PS教程:基础篇A
C#字体使用效果
Assembler Instructions with C Expression Operands
Common commands for starting services
Assembler Instructions with C Expression Operands
Lodash中数组和对象合并方法assign、merge、defaults、defaultsDeep比较
小记录
剑指offer - 从上到下打印二叉树 - (队列 结构体)
Swagger interface importing postman
Is there any age limit for opening an account? Excuse me, is it safe to open a stock account by mobile phone?
2022.7.19 simulation match
js实现一个Promises/A+ 规范的Promise
js类的创建和继承
Matlab GUI编程技巧(七):matlablistbox操作-列表框(ListBox)和uilistbox常用操作
side effect of intrinsics
Educational Codeforces Round 100 (Rated for Div. 2) B. Find The Array 题解
String... Variable length parameter list
Love running every day [noip2016 T4]