林大中被杀视频:字符串排序等算法

来源:百度文库 编辑:中财网 时间:2024/04/29 07:58:26

字符串排序等算法

分类: Delphi 2008-09-25 14:13 192人阅读 评论(0) 收藏 举报

15,1,5,10,13,14,50,4,55,8,67,68,69,3,12,57,70,74 字符串,排序后再把连续数字用-连接在一起,结果为:1,3-5,8,10,12-15,50,55,57,67-70,74

  1. function   NumberSort(List:   TStringList;   Index1,   Index2:   Integer):   Integer;
  2. var
  3.     Value1,Value2:Integer;
  4. begin
  5.     Value1:=StrToInt(List[Index1]);
  6.     Value2:=StrToInt(List[Index2]);
  7.     if   Value1        Result:=-1
  8.     else   if   Value1>Value2   then
  9.         Result:=1
  10.     else
  11.         Result:=0;
  12. end;
  13. procedure TForm1.btn3Click(Sender: TObject);
  14. var
  15.     strTemp:string;
  16.     strs:TStringList;
  17.     i,j:integer;
  18. begin
  19.     strTemp:='15,1,5,10,13,14,50,4,55,8,67,68,69,3,12,57,70,74';
  20.     strs:=TStringList.Create;
  21.     strs.Delimiter:=',';
  22.     strs.DelimitedText:=strTemp;
  23.     strs.CustomSort(NumberSort);
  24.     i:=1;
  25.     strTemp:=strs[0];
  26.     while true do
  27.     begin
  28.         if i>=strs.Count then
  29.             break;
  30.         if StrToInt(strs[i])-StrToInt(strs[i-1])=1 then
  31.         begin
  32.             for j:=i to strs.Count-2 do
  33.                 if StrToInt(strs[j+1])-StrToInt(strs[j])=1 then
  34.                     continue
  35.                 else
  36.                     break;
  37.             i:=j;
  38.             strTemp:=strTemp+'-'+strs[i];
  39.         end else
  40.             strTemp:=strTemp+','+strs[i];
  41.         inc(i);
  42.     end;
  43.     Memo1.Lines.Text:=strTemp;
  44.     strs.Free;
  45. end;