当前位置:网站首页>Tower of Hanoi 2 (function)
Tower of Hanoi 2 (function)
2022-07-19 06:54:00 【winkiii】
describe
the The classical tower of Hanoi problem often exists as a classic example of recursion . Maybe someone doesn't know the story of the tower of Hanoi problem . The tower of Hanoi comes from a story in Indian legend , God made three diamond pillars when he created the world Son , On a column, they are stacked in order of size from bottom to top 64 A golden disk . God ordered Brahman to rearrange the disc on another post in order of size from below . And stipulate , You can't put on a small disc Large disc , Only one disc can be moved between the three pillars at a time . There is a prophecy that , When this is done, the universe will be destroyed in a flash . Some people believe that Brahmans are still moving the disc all the time . Okay , When However, this legend is not believable , Nowadays, Hanoi Tower exists more as a toy .Gardon I received a Hanoi Tower toy as a birthday gift .
Gardon He is afraid of trouble ( Okay , Is a lazy person ), It is obvious that 64 It's difficult to move the plates one by one until all the plates reach the third pillar , therefore Gardon Decide to make a Minor malpractice , He found another exactly the same post , Move all the dishes to the third pillar faster through this pillar . The following question is : When Gardon Used in a game N null when , How many times does he need to move them to the third pillar ? Obviously , When there is no fourth pillar , The solution to the problem is 2^N-1, But now with the help of this pillar , How much should it be ?
Input
Contains multiple sets of data , One line per data , It's the number of plates N(1<=N<=64).
Output
For each set of data , Output a number , The minimum number of moves required to reach the target .
sample input 1
1
3
12
sample output 1
1
5
81
#include<stdio.h>
#include<math.h>
int main(){
int a[65];
int i,j,temp,min;
a[1] = 1;
a[2] = 3;
a[3] = 5;
for(i=4;i<65;i++){
min = 100000;
for(j=1;j<i;j++){
if((a[j]*2+pow(2,i-j)-1)<min){
min = a[j]*2+pow(2,i-j)-1;
}
}
a[i] = min;
}
int n;
while(scanf("%d",&n)!=EOF){
if(n==0) break;
printf("%d\n",a[n]);
}
}
边栏推荐
猜你喜欢

linxu下调试微信调一跳(Fedora 27)

Common user password encryption methods and cracking methods

高并发day04(ZAB协议,观察者,nc,AVRO,RPC)

Vcenter6.7 installation and troubleshooting

TCP/IP四层模型以及F5部分相关配置

Wu Enda machine learning chapter 10-11

C language structure array pointer and function

基于I2C的温度采集实验及实验心得

文本三劍客之awk命令--截取

F5 GTM(一):DNS参数
随机推荐
Pytorch deep learning practice-b station Liu erden-day6
redis
小迪网络安全-笔记(2)
关于STM汇编程序设计相关学习
Network layer and IP learning
高并发day01(NIO、ConCurrent包)
小迪-网络安全 笔记(1)
notepad++下划线以及大小写字母置换
聊聊中台:我对中台的一些理解与思考
渣渣学习之路(1)输出某年某月的日历页
【自动化测试】——robotframework实战(三)编写测试用例
Full experience of soft examination at the beginning, middle and advanced levels
F5ltm (I) logic diagram
Information on successful cooperation between CS brand sdnand and stm32mcu
简单irules编写 入门级
X11 forwarding
[CS Genesis] comparative analysis of advantages and disadvantages of SD NAND and raw NAND
Release nohup Out disk space occupied
Relevant knowledge points of Gugao motion control card
Wireshark packet capture: message information