Form1: TForm1;
GeLieZhiHe,GeHangZhiHe:array[0…1000] of integer;
intStringGrid1:array[0…1000,0…1000] of integer;
XingLa: boolean;
MaxNum,HuanFangNum:integer;
implementation
//uses
//Unit2;
{$R *.dfm}
{ 开始计算}
procedure TForm1.KaiShiJiSuan(Sender: TObject);
var
i,j,k:integer;
begin
for i:=0 to 1000 do
begin
GeLieZhiHe [i]:=0; // 各列之和
GeHangZhiHe[i]:=0;
end;
//幻方数
HuanFangNum:=StrToInt(Edit1.text) ;
// 最大数
MaxNum:=sqr(HuanFangNum );
// StringGrid1 方格行数,列数为幻方数
StringGrid1.ColCount:=HuanFangNum ;
StringGrid1.RowCount:=HuanFangNum;
{ intStringGrid1 方格全部填写为0}
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
intStringGrid1[i,j]:=0;
TianXieBenGe(0,0);
end;
{填写本格}
procedure TForm1.TianXieBenGe(ACol,ARow:integer);
var
// k :integer;
// i,j,k,Int1,Int2:integer;
{ 无相同数}
//function WuXiangTongShu:boolean;
//var
i,j,k,m,ANum:integer;
SuoYouDeShu1,SuoYouDeShu2:array[0…1000] of integer;
resut:boolean;
begin
//StringGrid1.Visible:=true;
//end;
//begin
//if result=false then
if ACol<=HuanFangNum-1 then
begin
for m:=1 to MaxNum do
begin
begin//-----------------------------------
for i:=0 to 1000 do
SuoYouDeShu1[i] :=0;
resut:=true;
k:=0;
// StringGrid1 中所有的数
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
begin
ANum:=intStringGrid1[i,j] ;
//for k:=0 to MaxNum do begin
SuoYouDeShu1[k] :=ANum;
inc(k);
if k=MaxNum then break;
// end;
end;
SuoYouDeShu2:=SuoYouDeShu1;
for i:=0 to MaxNum-1 do begin
for j:=0 to MaxNum-1 do begin
if i<>j then
if SuoYouDeShu1[i]=SuoYouDeShu2[j] then
begin
resut:=false;
break;
//showmessage('WuXiangTongShu=false;');
end;
end;
end;
end; //--------------------------------------------------------
if resut=true then
begin
winexec('C:\Program Files\SlowView\SlowView.exe C:\Program Files\acc\Wave\Ringin.wav',sw_shownormal);
//showmessage
break;
end;
if resut=false then
begin
//showmessage('无相同数false');
intStringGrid1[ACol,ARow]:=m;
// StringGrid1.Refresh;
// showmessage('StringGrid1 方格填写'+inttostr(k));
// Form2.Show;
// Form2.Hide;
TianXieXiaYiGe(ACol,ARow);
end;
end;
end;
end;
{填写下一格}
procedure TForm1.TianXieXiaYiGe(ACol,ARow:integer);
var
i,j,k,Int1,Int2:integer;
begin
i:=ACol;
j:=ARow+1;
if j=HuanFangNum then
begin
inc(i);
j:=0;
end;
int1:=i;
int2:=j;
TianXieBenGe(Int1,Int2 );
// TianXieBenGe(int1,int2);
end;
function TForm1.FuHeTiaoJian:boolean;
begin
result:=false;
// if WuXiangTongShu=true then
//if GeHangGeLieXiangTong=true then
result:=true;
end;
{ 验证各行各列 }
function TForm1.GeHangGeLieXiangTong:boolean;
var
i,j,k:integer;
// GeLieZhiHe,GeHangZhiHe:array[0…1000] of integer;
begin
result:=false;
for i:=0 to StringGrid1.ColCount-1 do
for j:=0 to StringGrid1.RowCount-1 do
GeLieZhiHe[i]:=GeLieZhiHe[i ]+ strtoint(StringGrid1.Cells[i,j]);
for i:=0 to StringGrid1.RowCount-1 do
for j:=0 to StringGrid1.ColCount-1 do
GeHangZhiHe[i]:=GeHangZhiHe[i ]+ strtoint(StringGrid1.Cells[i,j]);
for i:=0 to StringGrid1.ColCount-2 do begin
if GeLieZhiHe[i]=GeLieZhiHe[i+1] then begin
for j:=0 to StringGrid1.ColCount-1 do begin
if GeHangZhiHe[j]=GeHangZhiHe[j+1] then begin
if GeHangZhiHe[0]=GeLieZhiHe[0] then begin
result:=true;
end;
end;
end;
end;
end;
//
end;
{ 验证2} //
procedure TForm1.YanZheng2;
begin
showmessage(’ XingLa’); XingLa:=true;
// StringGrid1.Visible:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i,j,k,ANum:integer;
SuoYouDeShu1,SuoYouDeShu2:array[0…1000] of integer;
begin
//幻方数
HuanFangNum:=StrToInt(Edit1.text) ;
// 最大数
MaxNum:=sqr(HuanFangNum );
// StringGrid1 方格行数,列数为幻方数
StringGrid1.ColCount:=HuanFangNum ;
StringGrid1.RowCount:=HuanFangNum;
{ StringGrid1 方格全部填写为0}
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
StringGrid1.Cells[i,j]:=inttostr(0);
k:=0;
// StringGrid1 中所有的数
for i:=0 to HuanFangNum-1 do
for j:=0 to HuanFangNum-1 do
begin
ANum:=strtoint(StringGrid2.Cells[i,j]) ;
//for k:=0 to MaxNum do begin
SuoYouDeShu1[k] :=ANum;
inc(k);
if k=MaxNum then break;
// end;
end;
SuoYouDeShu2:=SuoYouDeShu1;
for i:=0 to MaxNum-1 do begin
for j:=0 to MaxNum-1 do begin
if i<>j then
if SuoYouDeShu1[i]=SuoYouDeShu2[j] then
begin
showmessage('WuXiangTongShu=false;');
break;
end;
end;
end;
//StringGrid1.Visible:=true;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
StringGrid2.Cells[0,0]:=inttostr(0);
StringGrid2.Cells[0,1]:=inttostr(1);
StringGrid2.Cells[1,0]:=inttostr(2);
StringGrid2.Cells[1,1]:=inttostr(30);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
StringGrid2.Cells[0,0]:=inttostr(0);
StringGrid2.Cells[0,1]:=inttostr(1);
StringGrid2.Cells[1,0]:=inttostr(1);
StringGrid2.Cells[1,1]:=inttostr(30);
end;