Delphi之汉诺塔算法求解
本帖最后由 bluesky5210 于 2014-9-27 16:33 编辑这是源代码:输入30个圆盘数量就卡在那了,有没有更高效的代码??还有如何测试一个程序执行的效率,就是想知道比如这个汉诺塔算法计算30个圆盘所需要的时间。?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure MoveHNTDisk(N:Integer;A,B,C:Char);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.MoveHNTDisk(N:Integer;A,B,C:Char);
begin
if N=1 then
begin
ListBox1.Items.Add(A +'--->' + C);
end
else
begin
MoveHNTDisk(N-1,A,C,B);
ListBox1.Items.Add(A + '--->' + C);
MoveHNTDisk(N-1,B,A,C);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Clear;
if StrToInt(Edit1.Text) > 0 then
begin
MoveHNTDisk(StrToInt(Edit1.Text),'A','B','C');
Label3.Caption:='所需次数为:'#10#13 + IntToStr(ListBox1.Items.Count) + '次';
end
else
ShowMessage('请输入大于0的整数!');
end;
end.
看看
页:
[1]