四塔问题Word文档格式.docx

上传人:b****5 文档编号:16648378 上传时间:2022-11-25 格式:DOCX 页数:7 大小:16.30KB
下载 相关 举报
四塔问题Word文档格式.docx_第1页
第1页 / 共7页
四塔问题Word文档格式.docx_第2页
第2页 / 共7页
四塔问题Word文档格式.docx_第3页
第3页 / 共7页
四塔问题Word文档格式.docx_第4页
第4页 / 共7页
四塔问题Word文档格式.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

四塔问题Word文档格式.docx

《四塔问题Word文档格式.docx》由会员分享,可在线阅读,更多相关《四塔问题Word文档格式.docx(7页珍藏版)》请在冰豆网上搜索。

四塔问题Word文档格式.docx

挺有意思啊,只是计算步数...

lvzhipu 

2009-9-1515:

01:

49

#include<

iostream>

using 

namespace 

std;

void 

move(char 

getone,char 

putone)

{

cout<

<

getone<

"

-->

putone<

endl;

}

hanoi(int 

n,char 

one,char 

two,char 

three)

putone);

if(n==1) 

move(one,three);

else

hanoi(n-1,one,three,two);

hanoi(n-1,two,one,three);

int 

main()

three);

m;

enter 

the 

number 

of 

diskes:

;

cin>

>

steps 

to 

moving"

m<

hanoi(m,'

A'

'

B'

C'

);

42

imjohnzj 

2008-10-1916:

30:

03

和刚才一样的代码,写紧凑点:

unsigned 

long 

hanno4(unsigned 

lv){

result=0,offset=1;

count=0,time=1,cur=0;

if(lv==0)return 

0;

while(lv--){

result+=offset;

cur++;

if(cur==time){time++;

cur=0;

offset+=offset;

result%=10000;

offset%=10000;

return 

result;

27:

14

只是为了求移动的步数嘛,不用真的一步步去移的啦。

if(lv 

== 

0)return 

while(lv 

--){

result 

+= 

offset;

cur 

++;

if(cur==time){

time 

offset 

%= 

10000;

woshicainiao 

2008-5-220:

42:

27

/***********************

Kn

和i<

=n

i=1

Kn是最大整数

1号桩下面Kn个固定,上面n-Kn个移到2号,

再把1号下面Kn个通过3移到4号

再把2号的n-Kn移动到4号

T(n)=2T(n-Kn)+2^Kn-1

令Rn=n-Kn*(Kn+1)/2

则T(n)=(Kn+Rn-1)*2^Kn+1

************************/

static 

Number=0;

iostream.h>

HannuoTower_3(int,char,char,char);

HannuoTower_4(int,char,char,char,char);

n;

char 

A='

B='

C='

D='

D'

HannuoTower_4(n,A,B,C,D);

**********************"

Number<

HannuoTower_3(int 

A,char 

B,char 

C)

Number++;

if(n==1)

A<

C<

HannuoTower_3(n-1,A,C,B);

HannuoTower_3(n-1,B,A,C);

HannuoTower_4(int 

C,char 

D)

D<

Kn=0;

S=0;

while(S+Kn+1<

=n)

S+=(++Kn);

HannuoTower_4(n-Kn,A,C,D,B);

HannuoTower_3(Kn,A,C,D);

HannuoTower_4(n-Kn,B,A,C,D);

尔冬四折 

2008-4-2121:

06:

34

x,char 

y,char 

z)

printf("

%c-->

%c\n"

x,z);

/* 

编号为1的圆盘从x移到z*/

hanoi(n-1,x,z,y);

将x上编号为1至n-1的圆盘移到y, 

z作辅助塔*/

将编号为n的圆盘从x移到z*/ 

hanoi(n-1,y,x,z);

/*将y上编号为1至n-1的圆盘移到z, 

x作辅助塔*/

main( 

Input 

disks:

scanf("

%d"

&

m);

he 

moving 

%3d 

\n"

m);

budognai 

2007-10-1919:

34:

45

强,虽然有点瑕疵

newjunwei 

2007-9-2120:

55:

13

有意思

shealo 

2007-8-415:

24:

51

源代码如下,已经验证正确性:

#include 

N;

total=0;

Single(int 

from, 

to, 

by, 

num)//三塔求解

a=0;

if(num==0)

a;

if(num==1)

//cout<

move 

from 

from<

to<

a+=1;

a+=Single(from,by,to,num-1);

a+=Single(by,to,from,num-1);

Double(int 

by1, 

by2, 

num)

a=1000000;

b=0;

for(int 

i=1;

i<

=num-1;

i++)

b+=Double(from,by1,by2,to,i);

b+=Single(from,by2,to,num-i-1);

b+=1;

b+=Single(by2,from,to,num-i-1);

b+=Double(by1,to,from,by2,i);

if(b<

a)

a=b;

main(int 

argc, 

char* 

argv[])

total=Double(1,4,2,3,N);

total<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 唐诗宋词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1