很久没提问了

daixiulin 2008-10-10 09:42:28
function StringInAry(str1,str2,str3:string;strSour:string):boolean;
begin
Result:=(Pos(strSour[1],str1)>0) and (Pos(strSour[2],str2)>0) and (Pos(strSour[3],str3)>0)
end;


function StringMaxCntInAry2(str1,str2,str3:string;strSour:TStrings):integer;
var
i,iMaxCnt,iCnt:Integer;
bFind,bStart:boolean;
begin
iCnt:=0;
iMaxCnt:=0;
bFind:=false;
bStart:=false;
for i:=strSour.Count-1 downto 0 do
begin
if StringInAry(str1,str2,str3,strSour.Strings[i]) then
begin
iMaxCnt:=Max(iMaxCnt,iCnt);
iCnt:=0;
bFind:=false;
bStart:=true;
end else
if bStart then
bFind:=true;

if bFind then
inc(iCnt);
end;
Result:=iMaxCnt;
end;

Temps2:=TStringList.Create;
temps2.add('123');
temps2.add('523');
temps2.add('127');
temps2.add('123');

MaxCnt:=StringMaxCntInAry2('1','2','3',Temps2); //////有问题?
求123出现的最大间隔?结果本应为3,却为0
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangsp 2008-10-10
  • 打赏
  • 举报
回复
function StringMaxCnt(str:string;strSour:TStrings):integer; 
var
i,d,max,p:integer;
begin
max:=-1;
p:=-1;
for i:=0 to strSour.Count-1 do
if strSour[i]=str then
begin
if p=-1 then p:=i else
begin
d:=i-p;
p:=i;
if max <d then max:=d;
end;
end;
result:=max;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
temps2:TStringList;
d:integer;
begin
Temps2:=TStringList.Create;
temps2.add('123');
temps2.add('523');
temps2.add('127');
temps2.add('123');
temps2.add('223');
temps2.add('523');
temps2.add('127');
temps2.add('123');
d:=StringMaxCnt('123',temps2);
showmessage(inttostr(d));
temps2.free;
end;
喝口水 2008-10-10
  • 打赏
  • 举报
回复
感觉你逻辑有问题,给你改一下
function StringMaxCnt(str:string;strSour:TStrings):integer;
var
i,d,max,p:integer;
begin
max:=-1;
p:=-1;
for i:=0 to strSour.Count-1 do
if strSour[i]=str then
begin
if p=-1 then p:=i else
begin
d:=i-p;
p:=i;
if max<d then max:=d;
end;
end;
result:=max;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
temps2:TStringList;
d:integer;
begin
Temps2:=TStringList.Create;
temps2.add('123');
temps2.add('523');
temps2.add('127');
temps2.add('123');
temps2.add('223');
temps2.add('523');
temps2.add('127');
temps2.add('123');
d:=StringMaxCnt('123',temps2);
showmessage(inttostr(d));
temps2.free;
end;
gzmhero 2008-10-10
  • 打赏
  • 举报
回复
代码是对的,这个结果是间隔=2。

16,742

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧