unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type Tlucheng = record FFrom: string; FTo: string; LiCheng: integer;//经过的路程 IsUse: boolean;//遍历标识 Citys: string;//经历过的城市 end;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; Edit2: TEdit; Button3: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure Button3Click(Sender: TObject); private ChinaInfo: array of Tlucheng; { Private declarations } function GetMaxCityLeng(s1, s2: string; len: integer; citys: string): string; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);begin Setlength(ChinaInfo, 10); ChinaInfo[0].FFrom := 'H'; ChinaInfo[0].FTo := 'B'; ChinaInfo[0].LICheng := 1200;//对应数据库的字段值 ChinaInfo[0].IsUse := false; //初始化为false ChinaInfo[0].Citys := '';//初始化为空 ChinaInfo[1].FFrom := 'H'; ChinaInfo[1].FTo := 'C'; ChinaInfo[1].LICheng := 200; ChinaInfo[1].IsUse := false; ChinaInfo[1].Citys := ''; ChinaInfo[2].FFrom := 'C'; ChinaInfo[2].FTo := 'B'; ChinaInfo[2].LICheng := 200; ChinaInfo[2].IsUse := false; ChinaInfo[2].Citys := ''; ChinaInfo[3].FFrom := 'B'; ChinaInfo[3].FTo := 'C'; ChinaInfo[3].LICheng := 200; ChinaInfo[3].IsUse := false; ChinaInfo[3].Citys := '';end;function TForm1.GetMaxCityLeng(s1, s2: string; len: integer; citys: string): string;var i: integer; MaxLen: integer; SCity: string;begin Result := ''; for i := 0 to length(ChinaInfo) - 1 do begin if not ChinaInfo.IsUse then begin if ChinaInfo.FFrom = s1 then begin ChinaInfo.IsUse := true; ChinaInfo.Citys := citys + '|' + s1; ChinaInfo.LICheng := len + ChinaInfo.LICheng; if ChinaInfo.FTo = s2 then begin ChinaInfo.Citys := ChinaInfo.Citys + '|' + s2; end else begin GetMaxCityLeng(ChinaInfo.FTo, s2, ChinaInfo.LICheng, ChinaInfo.Citys); end; end; end; end; MaxLen := 0; SCity := ''; for i := 0 to length(ChinaInfo) - 1 do begin if (ChinaInfo.IsUse) and (copy(ChinaInfo.Citys, 2, length(s1)) = s1) and (copy(ChinaInfo.Citys, length(ChinaInfo.Citys) - length(s2) + length('|'), length(s2)) = s2) then begin if ChinaInfo.LICheng > MaxLen then begin MaxLen := ChinaInfo.LICheng; SCity := ChinaInfo.Citys; end; end; end; Result := '经过的城市为:' + SCity + ' 路程为:' + inttostr(MaxLen);end;procedure TForm1.Button3Click(Sender: TObject);begin showmessage(GetMaxCityLeng('H', 'B', 0, ''));//调用end;end.//////////