就是讲编一个算法,假设n=3时,(n代表行、列)所以就是3行3列的一个格子里面
排列的数是1~9,是这样排列的:8 1 6
3 5 7
4 9 2
并且无论是一条斜线相加三个数,还是横向,纵向相加三个数,最终结果都是相等的
,怎么做?
排列的数是1~9,是这样排列的:8 1 6
3 5 7
4 9 2
并且无论是一条斜线相加三个数,还是横向,纵向相加三个数,最终结果都是相等的
,怎么做?
解决方案 »
- 关于Delphi TStrings的实现方式
- 求一份GSM MODEM的二次开发包啊
- 这条SQL语句在IB7中运行正常,但在FB1.5中却报错!
- Memo中如何将新添加的一行选中?
- 如何做一个对日和月进行点一下Updown增加或减少的日期?
- 我的Function要返回select * from MyTable的内容,请问返回的数据集是什么类型的变量?
- 怪哉,为何我这段代码不能限制鼠标移动区域?
- (CB版搞不定 DELPI版有没有高人指点?) CB程序员 请DELPI版inforpower高手指教!!!!!!!!
- 有懂MapX的吗? 拉兄弟一把!!!
- WPS2000专业版中打印反片的功能用DELPHI如何实现(我想打印反片报表),给点解决思路也行
- 只有60分了! ADO怎么连接sybase数据库
- EXE文件能用socket来传送吗?100分奉上
我扩展了一步,可以计算任意边长的矩阵。
我这个算法不是最佳算法,如果楼主有兴趣,我再改进unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}//计算边长为ABorderSize的矩阵,结果输出在AOutput上
procedure SortRetangle(ABorderSize: Integer; AOutput: TStrings);
var
NumUsed: array of Boolean; //数字是否用到,数组
Matrix: array of array of Integer; //矩阵,二维数组
Size: Integer; //矩阵边长,整数
MaxNum: Integer; //最大数字,整数
X, Y: Integer; //矩阵中的坐标 OutputStrings: TStrings; //输出的TStrings对象
ResultNum: Integer; //答案个数 //初始化
procedure Init;
var
I: Integer;
begin
Size := ABorderSize; MaxNum := Sqr(Size); SetLength(NumUsed, MaxNum + 1); SetLength(Matrix, Size);
for I := 0 to Size - 1 do
SetLength(Matrix[I], Size);
X := 0;
Y := 0; OutputStrings := AOutput;
ResultNum := 0;
end; //矩阵中(x,y)格的数字增加
//返回:可增加为有效数字,返回True,否则亦然
function Increase(AX, AY: Integer): Boolean;
var
PNum: ^Integer;
begin
PNum := @Matrix[AX, AY];
NumUsed[PNum^] := False;
repeat
Inc(PNum^);
if PNum^ > MaxNum then
begin
Result := False;
Exit;
end;
until not NumUsed[PNum^];
NumUsed[PNum^] := True;
Result := True;
end; //到矩阵的下一格
procedure Forward(var AX, AY: Integer);
begin
if AY >= Size - 1 then
begin
if AX >= Size - 1 then
AX := -1
else
Inc(AX);
AY := -1;
end;
Inc(AY);
end; //返回到矩阵的前一格
procedure Backward(var AX, AY: Integer);
begin
NumUsed[Matrix[AX, AY]] := False;
Matrix[AX, AY] := 0;
if AY <= 0 then
begin
Dec(AX);
AY := Size;
end;
Dec(AY);
end; //矩阵是否处理完最后一格
function FullFilled(AX, AY: Integer): Boolean;
begin
Result := (AX = Size - 1) and (AY = Size - 1);
end; //判断是否为答案
function CheckMatrix: Boolean;
var
I, J: Integer;
VSum, VValue, VResult: Integer;
begin
VSum := (1 + MaxNum) * MaxNum div 2;
VValue := VSum div Size;
Result := False; //行检查
for I := 0 to Size - 1 do
begin
VResult := 0;
for J := 0 to Size - 1 do
Inc(VResult, Matrix[I, J]);
if VResult <> VValue then
Exit;
end; //列检查
for I := 0 to Size - 1 do
begin
VResult := 0;
for J := 0 to Size - 1 do
Inc(VResult, Matrix[J, I]);
if VResult <> VValue then
Exit;
end; //斜线1检查
VResult := 0;
for I := 0 to Size - 1 do
Inc(VResult, Matrix[I, I]);
if VResult <> VValue then
Exit; //斜线2检查
VResult := 0;
for I := 0 to Size - 1 do
Inc(VResult, Matrix[I, Size - I - 1]);
if VResult <> VValue then
Exit; Result := True;
end; //输出结果
procedure OutputResult;
var
I, J: Integer;
begin
OutputStrings.Clear;
for I := 0 to Size - 1 do
begin
OutputStrings.Append('');
for J := 0 to Size - 1 do
OutputStrings[I] := OutputStrings[I] + Format('%5d', [Matrix[I, J]]);
end;
Inc(ResultNum);
ShowMessage(Format('第%d种答案', [ResultNum]));
end;
begin
//初始化
Init; while X > -1 do //穷举出口
begin
//当前格数字增加
if Increase(X, Y) then
//矩阵是否都以添上数字
if FullFilled(X, Y) then
begin
//检查是否为答案
if CheckMatrix then
//输出答案
OutputResult;
//回退一格
Backward(X, Y);
end
else
//前进一格
Forward(X, Y)
else
//回退一格
Backward(X, Y);
end;//while ShowMessage(Format('%d * %d 矩阵,共有%d种答案', [Size, Size, ResultNum]));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
SortRetangle(3, Memo1.Lines);
end;end.