112221111132222211111如何统计?

sixgj 2018-04-02 08:17:38
比如统计1这个数,如何统计连续最长的1的个数和连续的最短的1的个数?
...全文
514 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 的回复:
数据是字符串?
是一个字段.
多源动态最优潮流的分布鲁棒优化方法(IEEE118节点)(Matlab代码实现)内容概要:本文介绍了基于Matlab代码实现的多源动态最优潮流的分布鲁棒优化方法,适用于IEEE118节点电力系统。该方法结合两阶段鲁棒模型与确定性模型,旨在应对电力系统中多源不确定性(如可再生能源出力波动、负荷变化等),提升系统运行的安全性与经济性。文档还列举了大量相关的电力系统优化研究案例,涵盖微电网调度、电动汽车集群并网、需求响应、配电网重构等多个方向,并提供了YALMIP等工具包的网盘下载链接,支持科研复现与进一步开发。整体内容聚焦于电力系统建模、优化算法应用及鲁棒性分析。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事能源系统优化的工程技术人员;熟悉优化建模(如鲁棒优化、分布鲁棒优化)者更佳。; 使用场景及目标:①开展电力系统动态最优潮流研究,特别是含高比例可再生能源的场景;②学习和复现分布鲁棒优化在IEEE118等标准测试系统上的应用;③进行科研项目开发、论文复现或算法比较实验;④获取相关Matlab代码资源与仿真工具支持。; 阅读建议:建议按文档结构逐步浏览,重点关注模型构建思路与代码实现逻辑,结合提供的网盘资源下载必要工具包(如YALMIP),并在Matlab环境中调试运行示例代码,以加深对分布鲁棒优化方法的理解与应用能力。

16,743

社区成员

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

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