unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
const MAXN = 10;
procedure AddToResult(_a1, _a2, _a3, _b1, _b2, _b3, _c1, _c2, _c3: Integer);
procedure judge;
procedure search(const Depth: Integer);
var
Form1: TForm1;
MyArray: array[0..MAXN - 1] of Integer = (2, 4, 6, 8, 10, 11, 12, 14, 16, 18);
MyArray2: array[0..MAXN - 2] of Integer;
MyArray3: array[0..MAXN - 1] of Integer;
MyUsed: array[0..MAXN - 2] of Integer;
MySlt: TStringList;
implementation
{$R *.dfm}
procedure AddToResult(_a1, _a2, _a3, _b1, _b2, _b3, _c1, _c2, _c3: Integer);
var
MyStr: string;
begin
//MyStr :=
//MySlt.Add()
end;
procedure judge;
var
a1, a2, a3, b1, b2, b3, c1, c2, c3: Integer;
row1_sum, row2_sum, row3_sum, col1_sum, col2_sum, col3_sum: Integer;
begin
a1 := MyArray3[1];
a2 := MyArray3[2];
a3 := MyArray3[3];
b1 := MyArray3[4];
b2 := MyArray3[5];
b3 := MyArray3[6];
c1 := MyArray3[7];
c2 := MyArray3[8];
c3 := MyArray3[9];
row1_sum := a1 + a2 + a3;
row2_sum := b1 + b2 + b3;
row3_sum := c1 + c2 + c3;
col1_sum := a1 + b1 + c1;
col2_sum := a2 + b2 + c2;
col3_sum := a3 + b3 + c3;
if (row1_sum = 30) and (row2_sum = 30) and (row3_sum = 30) and
(col1_sum = 30) and (col2_sum = 30) and (col3_sum = 30) then
begin
//AddToResult(a1, a2, a3, b1, b2, b3, c1, c2, c3);
Form1.Memo1.Lines.Add(
inttostr(a1) + '**' +
inttostr(a2) + '**' +
inttostr(a3) + '**' +
inttostr(b1) + '**' +
inttostr(b2) + '**' +
inttostr(b3) + '**' +
inttostr(c1) + '**' +
inttostr(c2) + '**' +
inttostr(c3) + '**');
end;
end;
procedure search(const Depth: Integer);
var
I, j: Integer;
begin
for I := 0 to MAXN - 2 do
begin
if MyUsed = 0 then
begin
MyUsed := 1;
MyArray3[Depth] := MyArray2;
if Depth = 9 then
judge
else
search(Depth + 1);
MyUsed := 0;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I, j, k, tmp: Integer;
str: string;
begin
for I := 0 to MAXN - 1 do
begin
tmp := 0;
for j := 0 to MAXN - 2 do
begin
if tmp = I then
tmp := tmp + 1;
MyArray2[j] := MyArray[tmp];
tmp := tmp + 1;
end;
{ str := '';
For k := 0 to MAXN-2 do
str := str + inttostr(MyArray2[k]) + ' ';
memo1.Lines.Add(str);}
ZeroMemory(@MyUsed, MAXN - 2);
ZeroMemory(@MyArray3, MAXN - 1);
search(1);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MySlt := TStringList.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(MySlt) then
FreeAndNil(MySlt);
end;
end.
新建一个程序在窗体上放一个BUTTON,一个MEMO
就可以了.
^^