积极答复者
ACM的一个问题~

问题
-
做一道acm的编程题。题目是计算n层汉诺塔最少的移动次数。
我知道是2^n-1次。但是实际写代码时出了些小问题。
代码如下:
#include <iostream>
using namespace std;
void main()
{
__int64 n;
__int64 movesCount=1;
scanf("%I64d",&n);
for (int i=1;i<=n;i++)
movesCount*=2;
movesCount-=1;
printf("%I64d",movesCount);
}
上传到服务器测试,结果只拿了个98分。原因是最后的测试数值(n=64)计算结果不正确!
我在VS2008下编译,试了下64,果然,返回-1。
我看了下,i=64时,movesCount*=2;已经超过__int64的最大值了。
不知道这个问题怎么解决。
答案
-
Please see the reference of the limitation of integer types: http://msdn.microsoft.com/en-us/library/296az74e.aspx
Shuhai Shen - http://leonax.net- 已标记为答案 Nancy Shao 2010年5月17日 6:45
-
全部回复
-
Please see the reference of the limitation of integer types: http://msdn.microsoft.com/en-us/library/296az74e.aspx
Shuhai Shen - http://leonax.net- 已标记为答案 Nancy Shao 2010年5月17日 6:45
-