定义两个字符串数组 arrDate,arrCity分别存放日期和城市,数据如下:      arrDate     arrCity
  1   1月1日        北京
  2   1月2日        北京
  3   1月3日        北京
  4   1月4日        北京
  5   1月5日        上海
  6   1月6日        上海
  7   1月7日        广州
  8   1月8日        北京
  9   1月9日        北京
 10   1月10日       北京要在 memo1中显示的结果为: 1月1日 至 1月4日    北京
 1月5日 至 1月6日    上海
 1月7日 至 1月7日    广州
 1月8日 至 1月10日   北京

解决方案 »

  1.   

    分别遍历两个数组,因为索引都是对应的,遍历city时,记录开始,和结束索引,直到出现一个不一样的,取开始和结束索引处的Date值,就是了
      

  2.   

    遍历比较,遇到不同的就可以在memo中打印1行
      

  3.   

    procedure TForm1.FormCreate(Sender: TObject);
    var
      arrDate, arrCity: array[1..10] of string;
      I, B: Integer;
    begin
      arrDate[1] := '1月1日'; arrCity[1] := '北京';
      arrDate[2] := '1月2日'; arrCity[2] := '北京';
      arrDate[3] := '1月3日'; arrCity[3] := '北京';
      arrDate[4] := '1月4日'; arrCity[4] := '北京';
      arrDate[5] := '1月5日'; arrCity[5] := '上海';
      arrDate[6] := '1月6日'; arrCity[6] := '上海';
      arrDate[7] := '1月7日'; arrCity[7] := '广州';
      arrDate[8] := '1月8日'; arrCity[8] := '北京';
      arrDate[9] := '1月9日'; arrCity[9] := '北京';
      arrDate[10] := '1月10日'; arrCity[10] := '北京';  B := Low(arrCity);
      for I := Low(arrCity)+1 to High(arrCity)+1 do
        if (I = High(arrCity)+1) or (arrCity[I] <> arrCity[I-1]) then
        begin
          Memo1.Lines.Add(Format('%s 至 %s %s', [arrDate[B], arrDate[I-1], arrCity[I-1]]));
          B := I
        end;
    end;