112221111132222211111如何统计?

sixgj 2018-04-02 08:17:38
比如统计1这个数,如何统计连续最长的1的个数和连续的最短的1的个数?
...全文
498 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
好吧,用MaxInt可能安全一些。
lyhoo163 2018-04-05
  • 打赏
  • 举报
回复
可以加大值,设定10000
  • 打赏
  • 举报
回复
呃... 你这样处理也太草率了吧,如果恰好最少连续出现次数是100?
sixgj 2018-04-05
  • 打赏
  • 举报
回复
谢谢各位精彩回贴.
lyhoo163 2018-04-05
  • 打赏
  • 举报
回复
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));
  • 打赏
  • 举报
回复
引用 5 楼 sixgj 的回复:
[quote=引用 3 楼 DelphiGuy 的回复:] 字段也有类型,如果是字符型的话很好查找,数字型转成字符串再查找。
引用 3 楼 DelphiGuy 的回复:
字段也有类型,如果是字符型的话很好查找,数字型转成字符串再查找。 zif
字符型怎么查[/quote] 取到字符串中再查,比如: var s: string; //... s := xxx.FieldByName('字段名').AsString; // xxx是TDataSet派生类,诸如table或者query
sixgj 2018-04-04
  • 打赏
  • 举报
回复
引用 4 楼 lyhoo163 的回复:
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!
老兄这是如果是汉字怎么就查不到了?
sixgj 2018-04-04
  • 打赏
  • 举报
回复
引用 3 楼 DelphiGuy 的回复:
字段也有类型,如果是字符型的话很好查找,数字型转成字符串再查找。
引用 3 楼 DelphiGuy 的回复:
字段也有类型,如果是字符型的话很好查找,数字型转成字符串再查找。 zif
字符型怎么查
lyhoo163 2018-04-04
  • 打赏
  • 举报
回复
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!
  • 打赏
  • 举报
回复
如果查一个串中不存在的字符,你的代码L2会显示100,而不是0。
lyhoo163 2018-04-04
  • 打赏
  • 举报
回复
如果是汉字,将字符类型,改为widestring,上述代码,也行的。
  • 打赏
  • 举报
回复
字段也有类型,如果是字符型的话很好查找,数字型转成字符串再查找。
  • 打赏
  • 举报
回复
数据是字符串?
sixgj 2018-04-02
  • 打赏
  • 举报
回复
引用 1 楼 DelphiGuy 的回复:
数据是字符串?
是一个字段.

16,748

社区成员

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

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