求最优算法,急,请高手出招!!!
例:将字符串:1,3,4,5,8,10,12,13,14,15,50,55,57,67,68,69,70,74
最后经过好的方法转换为字符串:1,3-5,8,10,12-15,50,55,57,67-69,70,74 即把连续的数字用‘-’连接起来。
希望得到高手指点,需要详细具体的实现代码,非常感谢!!!!

解决方案 »

  1.   

    这很好实现啊
    function changeString(str:String):String;
    var
      i,iPos:integer;
      tempstr : String ;
    begin
      for i:=1 to Length(str) do 
        begin
          iPos := Pos(',',str);
          if iPos > 0 then 
             begin
               //依次比较每个字符的大小
             end;
        end;
    end;
      

  2.   


    function   NumberSort(List:   TStringList;   Index1,   Index2:   Integer):   Integer;
    var
        Value1,Value2:Integer;
    begin
        Value1:=StrToInt(List[Index1]);
        Value2:=StrToInt(List[Index2]);
        if   Value1<Value2   then
            Result:=-1
        else   if   Value1>Value2   then
            Result:=1
        else
            Result:=0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
        strTemp:string;
        strs:TStringList;
        i,j:integer;
    begin
        strTemp:='1,3,4,5,8,10,12,13,14,15,50,55,57,67,68,69,70,74';
        strs:=TStringList.Create;
        strs.Delimiter:=',';
        strs.DelimitedText:=strTemp;
        strs.CustomSort(NumberSort);    i:=1;
        strTemp:=strs[0];
        while true do
        begin
            if i>=strs.Count then
                break;
            if StrToInt(strs[i])-StrToInt(strs[i-1])=1 then
            begin
                for j:=i to strs.Count-2 do
                    if StrToInt(strs[j+1])-StrToInt(strs[j])=1 then
                        continue
                    else
                        break;
                i:=j;
                strTemp:=strTemp+'-'+strs[i];
            end else
                strTemp:=strTemp+','+strs[i];        inc(i);
        end;
        Memo1.Lines.Text:=strTemp;
        strs.Free;
    end;
      

  3.   

    4 楼 gzmhero 实在是高,佩服
      

  4.   

    结帖了,谢谢gzmhero 的鼎力相助,算法很有优秀,非常感谢!!!