unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
const
MaxNum = 36;
MinNum = 1;
type
TSetArray = array [1..6, 2..7] of Smallint;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Memo1: TMemo;
StringGrid2: TStringGrid;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure InitInputSg(Sg: TStringGrid);
procedure SgToArray(Sg: TStringGrid
var SubMin, SubMax: TSetArray);
procedure ArraryToSg(const SubMin, SubMax: TSetArray
Sg: TStringGrid);
function HandleTables(var SubMin, SubMax: TSetArray): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
InitInputSg(StringGrid1);
Button1.Click;
end;
procedure TForm1.InitInputSg(Sg: TStringGrid);
var
Step, j, L: integer;
begin
L := 0;
Sg.Cells[1, 0] := 'Min';
Sg.Cells[2, 0] := 'Max';
for Step := 1 to 6 do
for j := 1 to 7 - Step do
begin
L := L + 1;
Sg.Cells[0, L] := Format('%d - %d', [j, j + Step]);
Sg.Cells[1, L] := '';
Sg.Cells[2, L] := '';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
SubMin, SubMax: TSetArray;
i1, i2, i3, i4, i5, i6, i7, v: integer;
a, LowV, HighV: Array [1..7] of SmallInt;
function SetLoop(p: integer): Boolean;
var
i, v, HighP, LowP: integer;
begin
Result := false;
HighP := a[p - 1] + SubMax[p - 1, p];
if HighP > MaxNum + p - 7 then
HighP := MaxNum + p - 7;
LowP := a[1] + SubMin[1, p];
for i := 2 to p - 1 do
begin
v := a + SubMin[i, p];
if v > LowP then
begin
if v > HighP then Exit;
LowP := v;
end;
end;
HighV[p] := HighP;
LowV[p] := LowP;
Result := True;
end;
begin
SgToArray(StringGrid1, SubMin, SubMax);
if not HandleTables(SubMin, SubMax) then
begin
TButton(Sender).Caption := '数据错误';
Exit;
end;
ArraryToSg(SubMin, SubMax, StringGrid2);
Memo1.Lines.Clear;
v := 0;
for i1 := MinNum to MinNum + SubMax[1, 2] - 1 do
begin
a[1] := i1;
if not SetLoop(2) then Break;
for i2 := LowV[2] to HighV[2] do
begin
a[2] := i2;
if not SetLoop(3) then Break;
for i3 := LowV[3] to HighV[3] do
begin
a[3] := i3;
if not SetLoop(4) then Break;
for i4 := LowV[4] to HighV[4] do
begin
a[4] := i4;
if not SetLoop(5) then Break;
for i5 := LowV[5] to HighV[5] do
begin
a[5] := i5;
if not SetLoop(6) then Break;
for i6 := LowV[6] to HighV[6] do
begin
a[6] := i6;
if not SetLoop(7) then Break;
for i7 := LowV[7] to HighV[7] do
begin
a[7] := i7;
Inc(v);
if v < 100 then
Memo1.Lines.Add(Format('%2d %2d %2d %2d %2d %2d %2d',
[a[1], a[2], a[3], a[4], a[5], a[6], a[7]]));
end;
end;
end;
end;
end;
end;
end;
TButton(sender).Caption := Format('%d', [ v ]);
end;
procedure TForm1.SgToArray(Sg: TStringGrid
var SubMin, SubMax: TSetArray);
var
L, j, i, Step: integer;
begin
L := 0;
for j := 1 to 6 do
for i := 2 to 7 do
begin
SubMin[j, i] := 0;
SubMax[j, i] := 0;
end;
for Step := 1 to 6 do
for j := 1 to 7 - Step do
begin
L := L + 1;
if Sg.Cells[1, L] = '' then
SubMin[j, j + Step] := 1
else
SubMin[j, j + Step] := StrToInt(Sg.Cells[1, L]);
if StringGrid1.Cells[2, L] = '' then
SubMax[j, j + Step] := MaxNum - MinNum
else
SubMax[j, j + Step] := StrToInt(Sg.Cells[2, L]);
end;
end;
function TForm1.HandleTables(var SubMin, SubMax: TSetArray): Boolean;
var
i, j, Step, v: integer;
begin
Result := false;
for Step := 2 to 6 do //Set Min Table
for j := 1 to 7 - Step do
for i := 1 to Step - 1 do
begin
v := SubMin[j, j + i] + SubMin[j + i, j + Step];
if v > MaxNum - MinNum then Exit;
if v > SubMin[j, j + Step] then
SubMin[j, j + Step] := v;
end;
for Step := 6 downto 2 do //Set Max Table
for j := 1 to 7 - Step do
begin
v := SubMax[j, j + Step] - SubMin[j, j + 1];
if v < 1 then Exit;
if SubMax[j + 1, j + Step] > v then
SubMax[j + 1, j + Step] := v;
v := SubMax[j, j + Step] - SubMin[j + Step - 1, j + Step];
if v < 1 then Exit;
if SubMax[j, j + Step - 1] > v then
SubMax[j, j + Step - 1] := v;
end;
Result := True;
end;
procedure TForm1.ArraryToSg(const SubMin, SubMax: TSetArray;
Sg: TStringGrid);
var
Step, j, L: integer;
begin
L := 0;
Sg.Cells[0, 0] := 'FixedMin';
Sg.Cells[1, 0] := 'FixedMax';
for Step := 1 to 6 do
for j := 1 to 7 - Step do
begin
L := L + 1;
Sg.Cells[0, L] := Format('%.2d', [SubMin[j, j + Step]]);
Sg.Cells[1, L] := Format('%.2d', [SubMax[j, j + Step]]);
end;
end;
界面
object Form1: TForm1
Left = 145
Top = 134
Width = 608
Height = 397
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 384
Top = 0
Width = 128
Height = 370
Align = alRight
ColCount = 3
DefaultColWidth = 40
DefaultRowHeight = 14
RowCount = 22
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing]
TabOrder = 0
end
object Memo1: TMemo
Left = 0
Top = 0
Width = 145
Height = 370
Align = alLeft
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object StringGrid2: TStringGrid
Left = 512
Top = 0
Width = 88
Height = 370
Align = alRight
ColCount = 2
DefaultColWidth = 40
DefaultRowHeight = 14
FixedCols = 0
RowCount = 22
TabOrder = 2
end
object Button1: TButton
Left = 208
Top = 120
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 3
OnClick = Button1Click
end
end
//后来稍微修改了一下 HandleTables,请更新一下。