HDU-1996-汉诺塔VI

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1996

其实它就是求移动的所有可能,也就是n个盘子摆在三个塔上的任何可能的种数。可以这么思考这个问题:n个盘子分开摆在三个塔上,
所有可能的种数(这个和高中时候的一个信封投递到邮箱的问题很类似,那个是4封信投到3个邮箱,求投的种数的);n个盘子,每个
盘子有3种摆法,所以n个盘子摆在3个塔上的摆法就有3的n次幂种。知道这个规律之后,我们的问题就迎刃而解了。

代码

#include<stdio.h>
#include<math.h>

int main(void)
{
int n,t;
while(scanf("%d",&t)==1)
{
while(t--)
{
scanf("%d",&n);
printf("%I64d ",(__int64)(pow(3,n)+0.5));//精度处理因为3可能为2.999999999999,如果在强制转换就变成2了。

}
}
return 0;
}

代码

#include<stdio.h>
#include<math.h>

int main(void)
{
int t,n;
while(scanf("%d",&t)==1)
{
while(t--)
{
scanf("%d",&n);
printf("%.0lf ",pow(3,n));
}
}
return 0;
}

代码

#include<stdio.h>
#include<math.h>

int main(void)
{
int i;
for(i=1;i<=29;i++)
printf("%I64d %.0lf ",(__int64)(pow(3,i)),pow(3,i));
return 0;
}

输出

3 3
9 9
27 27
81 81
243 243
729 729
2187 2187
6561 6561
19683 19683
59049 59049
177147 177147
531441 531441
1594323 1594323
4782969 4782969
14348907 14348907
43046721 43046721
129140163 129140163
387420489 387420489
1162261467 1162261467
3486784401 3486784401
10460353202 10460353203
31381059609 31381059609
94143178826 94143178827
282429536481 282429536481
847288609442 847288609443
2541865828329 2541865828329
7625597484986 7625597484987
22876792454961 22876792454961
68630377364882 68630377364883

这就是区别。

原文地址:https://www.cnblogs.com/liudehao/p/4119342.html