描述
相传在印度的贝纳雷斯有座大寺庙,寺庙内有一块红木板,上面插着三根钻石棒,A,B,C,在盘古开天地,世界刚创造不久之时,神便在其中的一根钻石棒上放了64枚大小不一的纯金圆盘。有一个叫婆罗门的门徒,不分日夜地向这座寺庙赶路,抵达后,就尽力将64枚纯金的圆盘从A移到C钻石棒上,要求相邻之间的圆盘,在上方的要比在下方的小,且每天只能移动一块圆盘。等到婆罗门完成这项工作,寺庙和婆罗门本身都崩溃了,世界在一声霹雳中也毁灭了。现在要求你输出金盘数在23以内的,需要移动的天数。
格式
输入格式
输入n,表示n组测试数据,接下来输入n个数(均大于0小于等于23)。
输出格式
输出每组数据需要的天数,每组输出占一行。
样例
样例输入
4
11
13
8
14
样例输出
2047天
8191天
255天
16383天
提示
用递归解答
建议查看hanoi问题,然后设置一个静态变量count,不断得到这个移动次数大小
https://www.cnblogs.com/destiny1123/articles/4242398.html
// This program displays a solution to the towers of Hanoi game.
#include <iostream>
#include <string>
using namespace std;
// Function prototype
void moveDisks(int, string, string, string);
int main()
{
// Play the game with 3 disks
moveDisks (3, "peg 1", "peg 3", "peg 2");
cout << "All the disks have been moved!";
return 0;
}
void
moveDisks(int n, string source, string dest, string temp)
{
if (n > 0)
{
// Move n-1 disks from source to temp
// using dest as the temporary peg
moveDisks(n-1, source, temp, dest);
// Move a disk from source to dest
cout << "Move a disk from " << source << " to " << dest << endl;
// Move n-1 disks from temp to dest
// using.source as the temporary peg
moveDisks(n-1, temp, dest, source);
}
}