[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
相传山上有个庙,庙里有个小和尚和老和尚。一天小徒弟闲来无事,就去找老和尚,问有没有什么可以锻炼智力的游戏,老和尚就给小和尚准备了三根柱子,说:首先第一根柱子上只有一个碗,你吧这个碗挪到第三个柱子上,小和尚很轻松的就完成了;然后老和尚又在第一根柱子上放了三个碗,而且这三个碗是按从小到大依次放,他这次要求小和尚把这三个碗从第一根柱子挪到第三根柱子上,但是挪动的过程中得保证小碗在大碗之上,且每次只能移动一个碗,小和尚思索片刻后,完成了目标;但是最后老和尚说我要是给你64个碗呢?
小和尚陷入了思考。聪明的你知道怎么做嘛?
汉诺塔(Hanoi
Tower),又称河内塔,源于印度一个古老传说。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,任何时候,在小圆盘上都不能放大圆盘,且在三根柱子之间一次只能移动一个圆盘。问应该如何操作?
1、挪一个盘子: (1步)
小朋友都知道的,当然是直接放过去就好了 A - - > C
2、挪两个盘子:(3步)
A1- - > B , A2 - - > C :
A1 - - > C :
3、挪三个盘子: (7步)
A1 - - > C , A2 - - > B :
A1 - - > B , A3 - - > C :
A1 - - > A , A2 - - > C , A1 - - > C
我们总结以上的规律 :
挪一个盘子: 1步 = 2^1 - 1
挪两个盘子: 3步 = 2^2 - 1
挪三个盘子: 7步 = 2^3 - 1
…
挪64个盘子: 2^64 - 1
我们简单计算一下,计算机运行64个盘子得运行将近 300 年左右
所以我们简单的输出一下1、2、3个盘子的结果
package test24; public class testDemo { public static void move (char pos1,char
pos2) { System.out.print(pos1+" --> "+pos2+" ");//模仿鼠标的移动 } /** * * @param n
代表盘子的数目 * @param pos1 代表第一根柱子 A * @param pos2 代表第二根柱子 B * @param pos3 代表第三根柱子 C
*/ public static void HanoiTower (int n,char pos1,char pos2,char pos3) {
//n代表盘子的数目 if(n == 1) { move(pos1,pos3); } else { HanoiTower(n-1,pos1,pos3,pos2)
; // pos1 上的东西通过 pos3 传到 pos2 上 move(pos1,pos3); HanoiTower(n-1,pos2,pos1,pos3);
} } public static void main(String[] args) { HanoiTower(1,'A','B','C'); System.
out.println(); HanoiTower(2,'A','B','C'); System.out.println(); HanoiTower(3,'A'
,'B','C'); System.out.println(); } }
运行结果:
小伙伴想尝试 64 个盘子的,可以自己试试哦!!!