首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

The Tower of Hanoi 当输入32之后的数字int型没有输出,求大神C++代码,

2012-09-07 
The Tower of Hanoi 当输入32以后的数字int型没有输出,求大神C++代码,急!!DescriptionYou are given a tow

The Tower of Hanoi 当输入32以后的数字int型没有输出,求大神C++代码,急!!
Description
You are given a tower of n disks, initially stacked in decreasing size on one of three pegs, your objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never moving a larger one onto a smaller. For example, n = 2:



We need 3 moves to perform the task.


Now the question arises: What's the best we can do? That is, how many moves are necessary and sufficient to perform the task? 

Please write a program to find out the minimum number of moves we need to do with n disks in the tower.


Input
One positive integer, n (1 <= n <= 64).



Output
One integer, the minimum number of moves needed.


[解决办法]
这个问题的公式是2^n-1
当n>=32时就不能再用int了,按题目要求看,n最大是64,最起码要用unsigned long long

C/C++ code
unsigned long long HanoiStep(unsigned short n){    return (((unsigned long long)1) << n) - 1;}int main(){    unsigned short n;    while (cin>>n)    {        cout<<HanoiStep(n)<<endl;    }    system("pause");    return 0;} 

热点排行
Bad Request.