汉诺塔2 这个其实和汉诺塔没什么两样 就是最优策略需要循环判断
想一想其实最后也就是分成两坨:一坨已经就位(是比较大的块)另一坨还在另一个杆子上
但是我们不知道是多大的时候能走到最小步数 所以我们需要循环判断
但是对于pow的使用还是要着重强调
如果直接
pow(2,i-j)-1);oj就会分不清第一个传入的是int float or double 类型 所以要用2.0 来确定类型
这个错误是
<pre>0_0_16264803_1999.cpp(15) : error C2668: “pow”: 对重载函数的调用不明确
2013\INCLUDE\math.h(1233): 可能是“long double pow(long double,int) throw()”
2013\INCLUDE\math.h(1231): 或 “long double pow(long double,long double) throw()”
2013\INCLUDE\math.h(1117): 或 “float pow(float,int) throw()”
2013\INCLUDE\math.h(1115): 或 “float pow(float,float) throw()”
2013\INCLUDE\math.h(1027): 或 “double pow(double,int) throw()”
2013\INCLUDE\math.h(512): 或 “double pow(double,double)”
尝试匹配参数列表“(int, int)”时
ac代码如下
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
long long hanb[65];
long long min;
int C;
hanb[1]=1;
for(int i=2;i<65;i++)
{
min=9999999999;
for(int j=1;j<i;j++)
{
if((hanb[j]*2+pow(2.0,i-j)-1)<min)
min=hanb[j]*2+pow(2.0,i-j)-1;
}
hanb[i]=min;
}
while(scanf("%d",&C)!=EOF)
{
printf("%lld\n",hanb[C]);
}
return 0;
}