16,748
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
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.