16,748
社区成员
发帖
与我相关
我的任务
分享
procedure TForm1.Button1Click(Sender: TObject);
var S,S1,s2:string;
i,L,L1,L2:integer;
befor,now,T:boolean;
begin
S:=Edit1.text;//r'112221111132222211111122111111111233';
befor:=True;
L1:=0; l2:=100; L:=0;
for i:= 1 to length(S) do
begin
if S[i]='1' then
begin
if befor then L:=L+1 // 记录长度
else L:=1;
if L>L1 then L1:=L;
befor:=True;
end
else begin
if (L<L2) and (L>0) then L2:=L;
L:=0; // 重新记数
befor:=False;
end;
if i=length(S) then
begin
if (L<L2) and (L>0) then L2:=L;
end;
end;
if L2=100 then L2:=0;
Edit2.text:=Inttostr(L1);
Edit3.text:=Inttostr(L2);
end;
人是活的的,修改一下就可以了。
uses System.StrUtils;
type
TStringHelper = record helper for string
function SequentialStrCount(const ASubString: string; CountMax: boolean = true): integer;
end;
function TStringHelper.SequentialStrCount(const ASubString: string; CountMax: boolean = true): integer;
var
n, p, r: integer;
begin
Result := 0;
n := Length(ASubString);
if n = 0 then exit;
p := 1;
repeat
p := PosEx(ASubString, Self, p);
if p = 0 then break;
r := 1;
repeat
p := p + n;
if Copy(Self, p, n) = ASubString then Inc(r)
else break;
until false;
case CountMax of
true:
if r > Result then Result := r;
false:
if Result = 0 then Result := r
else if r < Result then Result := r;
end;
until false;
end;
// 使用:以上代码放到任何可以引用到的单元(当前文件也可以),然后就可以:
var
s: string;
//...
s := '112221111132222211111';
writeln(s.SequentialStrCount('1'));
writeln(s.SequentialStrCount('1', false));
//或者直接:
writeln('112221111132222211111'.SequentialStrCount('1'));
writeln('112221111132222211111'.SequentialStrCount('没有找到', false));
procedure TForm1.Button1Click(Sender: TObject);
var S,S1,s2:string;
i,L,L1,L2:integer;
befor,now,T:boolean;
begin
S:=Edit1.text;//r'112221111132222211111122111111111233';
befor:=True;
L1:=0; l2:=100; L:=0;
for i:= 1 to length(S) do
begin
if S[i]='1' then
begin
if befor then L:=L+1 // 记录长度
else L:=1;
if L>L1 then L1:=L;
befor:=True;
end
else begin
if (L<L2) and (L>0) then L2:=L;
L:=0; // 重新记数
befor:=False;
end;
if i=length(S) then
begin
if (L<L2) and (L>0) then L2:=L;
end;
end;
Edit2.text:=Inttostr(L1);
Edit3.text:=Inttostr(L2);
end;
OK!