吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2563|回复: 9
收起左侧

[其他原创] 汉诺塔问题的解决

[复制链接]
KaQqi 发表于 2019-3-19 18:17
刚刚学完递归,就做这一道题,做的头疼。。
某编程大神给了一个图。。

我真敬佩这位前辈,他是怎么想到把前63个看为一个整体的。。

在前辈的指点下,终于写出来了。。。
[C++] 纯文本查看 复制代码
#include <stdio.h>

class plate
{
public:
	void move(int count,plate start,plate end,plate middle_use)
	{
	if(count != 1)
	{
		move(count-1,start,middle_use,end);
		move(1,start,end,middle_use);//第四个参数可以乱传
		move(count-1,middle_use,end,start); 
	}
	if(count == 1)
	{
		printf("%c-%c\n",start.position,end.position);
	}
	}
	void givePosition(char character)
	{
		this->position = character;
	}
private:
	char position;
};

//void move(int count,plate start,plate end,plate middle_use)
//{
//	if(count != 1)
//	{
//		move(count-1,start,middle_use,end);
//		move(1,start,end,middle_use);//第四个参数可以乱传
//		move(count-1,middle_use,end,start); 
//	}
//	if(count == 1)
//	{
//		printf("%c-%c\n",start.position,end.position);
//	}
//}

int main()
{
	int count;
	scanf("%d",&count);
	plate a;
	plate start,end,middle_use;
	start.givePosition('A');
	end.givePosition('C');
	middle_use.givePosition('B');
	a.move(count,start,end,middle_use);
	return 0;
}









不得不说没有这个图的提醒我真写不出来。。

我好菜啊

免费评分

参与人数 1热心值 +1 收起 理由
yywj + 1 同感

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

JieW_L 发表于 2019-3-19 18:55
我记得这玩意,PY只要三行啊
WJJIahUa 发表于 2019-3-19 19:09
涛之雨 发表于 2019-3-19 19:31
苏紫方璇 发表于 2019-3-19 19:34
所以说图呢
 楼主| KaQqi 发表于 2019-3-19 20:10
本帖最后由 cqr2287 于 2019-3-19 20:14 编辑

链接gg了。。待会我用电脑传上去
bester 发表于 2019-3-20 13:40
我记得我看过一个视频,大神是用二进制解决这个汉诺塔问题的,对没错,就是0和1
苏紫方璇 发表于 2019-3-20 14:04
 楼主| KaQqi 发表于 2019-3-20 19:45
苏紫方璇 发表于 2019-3-20 14:04
https://blog.csdn.net/qq_37873310/article/details/80461767
图片出处应该是这个

哦,您怎么知道的
苏紫方璇 发表于 2019-3-20 19:54
百度一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 00:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表