有数组A,A[0..99]有100个数(或者更多),想求出这样几对数,满足y1-y2在某个阈值内,x1-x2在某个阈值内,x1,x2表示第几个,y1,y2表示x1,x2对应的值,不知道用什么方法能快速实现,请各位大侠帮帮忙啊 在线等

解决方案 »

  1.   

    y1,y2表示x1,x2对应的值
    什么意思
      

  2.   

    参考:
    http://apps.hi.baidu.com/share/detail/6227165
      

  3.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Grids, DBGrids;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        Edit3: TEdit;
        Edit4: TEdit;
        StringGrid1: TStringGrid;
        procedure Search(IndexMin, IndexMax, ValueMin, ValueMax: Integer);
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private 愰尵 }
      public
        { Public 愰尵 }
      end;var
      Form1: TForm1;implementation{$R *.DFM}const
      A: Array[0..9] of Integer = (0,44,11,61,22,74,33,87,44,100);procedure TForm1.Search(IndexMin, IndexMax, ValueMin, ValueMax: Integer);
    var
      I: Integer;
      x1, x2: Integer;
      y1, y2: Integer;
    begin
      // 清空StringGrid1
      for I := 1 to ( StringGrid1.RowCount - 1 ) do
      begin
        StringGrid1.Rows[I].Clear;
        StringGrid1.RowCount := 0;
      end;
      // 查找符合要求的组合
      for x2 := 0 to ( IndexMax - IndexMin -1 ) do
      begin
        for x1 := ( x2 + 1 ) to ( IndexMax - IndexMin ) do
        begin
          // x1-x2的值在给定阈值内
          if ( ( x1 - x2 ) >= IndexMin ) and ( ( x1 - x2) <= IndexMax ) then
          begin
            y1 := A[x1];
            y2 := A[x2];
            // y1-y2的值在给定的阈值内
            if ( ( y1 - y2 ) >= ValueMin ) and ( ( y1 - y2 ) <= ValueMax ) then
            begin
              with StringGrid1 do
              begin
                RowCount := RowCount + 1;
                // 向StringGrid1中添加数据
                Cells[0,RowCount-1] := IntToStr(x1);
                Cells[1,RowCount-1] := IntToStr(x2);
                Cells[2,RowCount-1] := IntToStr(y1);
                Cells[3,RowCount-1] := IntToStr(y2);
              end;
            end;
          end;
        end;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      IndexMin, IndexMax: Integer; // x1-x2的最小值和最大值
      ValueMin, ValueMax: Integer; // y1-y2的最小值和最大值
    begin
      IndexMin := StrToInt(Trim(Edit1.Text));
      IndexMax := StrToInt(Trim(Edit2.Text));
      ValueMin := StrToInt(Trim(Edit3.Text));
      ValueMax := StrToInt(Trim(Edit4.Text));
      Search(IndexMin, IndexMax, ValueMin, ValueMax);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      // 初始化StringGrid1
      with StringGrid1 do
      begin
        ColCount := 4;
        RowCount := 0;
        StringGrid1.FixedCols := 2;
        FixedRows := 0;
        Cells[0,0] := 'x1';
        Cells[1,0] := 'x2';
        Cells[2,0] := 'y1';
        Cells[3,0] := 'y2';
      end;
    end;end.
      

  4.   

    学习学习先
    继续顶一下
    顶顶帖子高手们
    也请多多赐教
    http://topic.csdn.net/u/20110709/12/3ec63017-cef8-41e0-a524-c9b9dfe56d5c.html
    http://topic.csdn.net/u/20110611/12/3258c959-4f28-46b7-b5d6-46135d73036b.html
    http://topic.csdn.net/u/20110722/14/89f7440b-c4d7-4c9a-a4bb-a503f5135db2.html
    http://topic.csdn.net/u/20110729/10/a7bfaf06-0cf9-4580-8e91-d4e0b92066c6.htmlhttp://topic.csdn.net/u/20110811/16/e56e7cc1-d8c9-40af-92e3-c24ca103d17d.html
    http://topic.csdn.net/u/20110830/13/dfae4ca5-d2b9-4889-8a3c-6f7fb61936c9.html
    http://topic.csdn.net/u/20110831/17/427b6ab0-66c6-4f33-af26-ebd27b0dc541.html
    http://topic.csdn.net/u/20110901/09/83c2c668-cbe6-4db9-966d-bf2f47456811.html
    http://topic.csdn.net/u/20110905/12/a1161adb-8e5d-491a-b302-c9722edf2dab.html
    http://topic.csdn.net/u/20110906/10/86d49517-b7a6-4fae-982b-478d2d6e2a23.html