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
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;}