请教各位给做段程序?(200分)

  • 主题发起人 主题发起人 super-ljc
  • 开始时间 开始时间
S

super-ljc

Unregistered / Unconfirmed
GUEST, unregistred user!
有这样一个问题请教大家用DELPHI如何实现? 有 2 4 6 8
10 11 12 14 16 18 数填在一个3×3的格子中使每行每列相加都得30请问各位高手应如何用DELLPHI来实现呢?
 
我以想知道其实排法不难,但怎么用Delphi实现就...
 
有人能提供如何做的方法也可
 
穷举法,大约36万次计算就完成了
 
再拿200分来,给你玩玩,包教包会呀
 
哈! 2 4 6 8 10 11 12 14 16 18共计10个数,如何填在3x3=9个格子里? 有一个格子可以放2个数吗?
 
那个11是多出来的吗?
 
共有10个数,只放9个数进去是吧.
 
只放9个没要你放10个这是一个小学二年级学奥数数学题!
 
应该是 不可以重复的吧
如果可以重复 ,全填 10吧 呵呵
如果 这样 我觉得 11可以排除了啊 咋整 都是单啊
这个好象得 利用 线性代数中的行列式吧
x1 y1 z1 =30
x2 y2 z2 =30
x3 y3 z3 =30
30 30 30
线性代数忘了啊 嘎嘎..
 
设第一行的3个数按顺序为: top1; top2; top3;
设第二行的3个数按顺序为:middle1;middle2;middle3;
设第三行的3个数按顺序为:bottom1;bottom2;bottom3;
如果要每行每列相加都等于30的话,如下:
答案1(top1=2 top2=10 top3=18; middle1=12 middle2=14 middle3=4; bottom1=16 bottom2=6 bottom3=8)
答案2(top1=2 top2=10 top3=18; middle1=16 middle2=6 middle3=8; bottom1=12 bottom2=14 bottom3=4)
答案3(top1=2 top2=12 top3=16; middle1=10 middle2=14 middle3=6; bottom1=18 bottom2=4 bottom3=8)
答案4(top1=2 top2=12 top3=16; middle1=18 middle2=4 middle3=8; bottom1=10 bottom2=14 bottom3=6)
答案5(top1=2 top2=16 top3=12; middle1=10 middle2=6 middle3=14; bottom1=18 bottom2=8 bottom3=4)
答案6(top1=2 top2=16 top3=12; middle1=18 middle2=8 middle3=4; bottom1=10 bottom2=6 bottom3=14)
答案7(top1=2 top2=18 top3=10; middle1=12 middle2=4 middle3=14; bottom1=16 bottom2=8 bottom3=6)
答案8(top1=2 top2=18 top3=10; middle1=16 middle2=8 middle3=6; bottom1=12 bottom2=4 bottom3=14)
答案9(top1=4 top2=8 top3=18; middle1=12 middle2=16 middle3=2; bottom1=14 bottom2=6 bottom3=10)
答案10(top1=4 top2=8 top3=18; middle1=14 middle2=6 middle3=10; bottom1=12 bottom2=16 bottom3=2)
答案11(top1=4 top2=12 top3=14; middle1=8 middle2=16 middle3=6; bottom1=18 bottom2=2 bottom3=10)
答案12(top1=4 top2=12 top3=14; middle1=18 middle2=2 middle3=10; bottom1=8 bottom2=16 bottom3=6)
答案13(top1=4 top2=14 top3=12; middle1=8 middle2=6 middle3=16; bottom1=18 bottom2=10 bottom3=2)
答案14(top1=4 top2=14 top3=12; middle1=18 middle2=10 middle3=2; bottom1=8 bottom2=6 bottom3=16)
答案15(top1=4 top2=18 top3=8; middle1=12 middle2=2 middle3=16; bottom1=14 bottom2=10 bottom3=6)
答案16(top1=4 top2=18 top3=8; middle1=14 middle2=10 middle3=6; bottom1=12 bottom2=2 bottom3=16)
答案17(top1=6 top2=8 top3=16; middle1=10 middle2=18 middle3=2; bottom1=14 bottom2=4 bottom3=12)
答案18(top1=6 top2=8 top3=16; middle1=14 middle2=4 middle3=12; bottom1=10 bottom2=18 bottom3=2)
答案19(top1=6 top2=10 top3=14; middle1=8 middle2=18 middle3=4; bottom1=16 bottom2=2 bottom3=12)
答案20(top1=6 top2=10 top3=14; middle1=16 middle2=2 middle3=12; bottom1=8 bottom2=18 bottom3=4)
答案21(top1=6 top2=14 top3=10; middle1=8 middle2=4 middle3=18; bottom1=16 bottom2=12 bottom3=2)
答案22(top1=6 top2=14 top3=10; middle1=16 middle2=12 middle3=2; bottom1=8 bottom2=4 bottom3=18)
答案23(top1=6 top2=16 top3=8; middle1=10 middle2=2 middle3=18; bottom1=14 bottom2=12 bottom3=4)
答案24(top1=6 top2=16 top3=8; middle1=14 middle2=12 middle3=4; bottom1=10 bottom2=2 bottom3=18)
答案25(top1=8 top2=4 top3=18; middle1=6 middle2=14 middle3=10; bottom1=16 bottom2=12 bottom3=2)
答案26(top1=8 top2=4 top3=18; middle1=16 middle2=12 middle3=2; bottom1=6 bottom2=14 bottom3=10)
答案27(top1=8 top2=6 top3=16; middle1=4 middle2=14 middle3=12; bottom1=18 bottom2=10 bottom3=2)
答案28(top1=8 top2=6 top3=16; middle1=18 middle2=10 middle3=2; bottom1=4 bottom2=14 bottom3=12)
答案29(top1=8 top2=16 top3=6; middle1=4 middle2=12 middle3=14; bottom1=18 bottom2=2 bottom3=10)
答案30(top1=8 top2=16 top3=6; middle1=18 middle2=2 middle3=10; bottom1=4 bottom2=12 bottom3=14)
答案31(top1=8 top2=18 top3=4; middle1=6 middle2=10 middle3=14; bottom1=16 bottom2=2 bottom3=12)
答案32(top1=8 top2=18 top3=4; middle1=16 middle2=2 middle3=12; bottom1=6 bottom2=10 bottom3=14)
答案33(top1=10 top2=2 top3=18; middle1=6 middle2=16 middle3=8; bottom1=14 bottom2=12 bottom3=4)
答案34(top1=10 top2=2 top3=18; middle1=14 middle2=12 middle3=4; bottom1=6 bottom2=16 bottom3=8)
答案35(top1=10 top2=6 top3=14; middle1=2 middle2=16 middle3=12; bottom1=18 bottom2=8 bottom3=4)
答案36(top1=10 top2=6 top3=14; middle1=18 middle2=8 middle3=4; bottom1=2 bottom2=16 bottom3=12)
答案37(top1=10 top2=14 top3=6; middle1=2 middle2=12 middle3=16; bottom1=18 bottom2=4 bottom3=8)
答案38(top1=10 top2=14 top3=6; middle1=18 middle2=4 middle3=8; bottom1=2 bottom2=12 bottom3=16)
答案39(top1=10 top2=18 top3=2; middle1=6 middle2=8 middle3=16; bottom1=14 bottom2=4 bottom3=12)
答案40(top1=10 top2=18 top3=2; middle1=14 middle2=4 middle3=12; bottom1=6 bottom2=8 bottom3=16)
答案41(top1=12 top2=2 top3=16; middle1=4 middle2=18 middle3=8; bottom1=14 bottom2=10 bottom3=6)
答案42(top1=12 top2=2 top3=16; middle1=14 middle2=10 middle3=6; bottom1=4 bottom2=18 bottom3=8)
答案43(top1=12 top2=4 top3=14; middle1=2 middle2=18 middle3=10; bottom1=16 bottom2=8 bottom3=6)
答案44(top1=12 top2=4 top3=14; middle1=16 middle2=8 middle3=6; bottom1=2 bottom2=18 bottom3=10)
答案45(top1=12 top2=14 top3=4; middle1=2 middle2=10 middle3=18; bottom1=16 bottom2=6 bottom3=8)
答案46(top1=12 top2=14 top3=4; middle1=16 middle2=6 middle3=8; bottom1=2 bottom2=10 bottom3=18)
答案47(top1=12 top2=16 top3=2; middle1=4 middle2=8 middle3=18; bottom1=14 bottom2=6 bottom3=10)
答案48(top1=12 top2=16 top3=2; middle1=14 middle2=6 middle3=10; bottom1=4 bottom2=8 bottom3=18)
答案49(top1=14 top2=4 top3=12; middle1=6 middle2=8 middle3=16; bottom1=10 bottom2=18 bottom3=2)
答案50(top1=14 top2=4 top3=12; middle1=10 middle2=18 middle3=2; bottom1=6 bottom2=8 bottom3=16)
答案51(top1=14 top2=6 top3=10; middle1=4 middle2=8 middle3=18; bottom1=12 bottom2=16 bottom3=2)
答案52(top1=14 top2=6 top3=10; middle1=12 middle2=16 middle3=2; bottom1=4 bottom2=8 bottom3=18)
答案53(top1=14 top2=10 top3=6; middle1=4 middle2=18 middle3=8; bottom1=12 bottom2=2 bottom3=16)
答案54(top1=14 top2=10 top3=6; middle1=12 middle2=2 middle3=16; bottom1=4 bottom2=18 bottom3=8)
答案55(top1=14 top2=12 top3=4; middle1=6 middle2=16 middle3=8; bottom1=10 bottom2=2 bottom3=18)
答案56(top1=14 top2=12 top3=4; middle1=10 middle2=2 middle3=18; bottom1=6 bottom2=16 bottom3=8)
答案57(top1=16 top2=2 top3=12; middle1=6 middle2=10 middle3=14; bottom1=8 bottom2=18 bottom3=4)
答案58(top1=16 top2=2 top3=12; middle1=8 middle2=18 middle3=4; bottom1=6 bottom2=10 bottom3=14)
答案59(top1=16 top2=6 top3=8; middle1=2 middle2=10 middle3=18; bottom1=12 bottom2=14 bottom3=4)
答案60(top1=16 top2=6 top3=8; middle1=12 middle2=14 middle3=4; bottom1=2 bottom2=10 bottom3=18)
答案61(top1=16 top2=8 top3=6; middle1=2 middle2=18 middle3=10; bottom1=12 bottom2=4 bottom3=14)
答案62(top1=16 top2=8 top3=6; middle1=12 middle2=4 middle3=14; bottom1=2 bottom2=18 bottom3=10)
答案63(top1=16 top2=12 top3=2; middle1=6 middle2=14 middle3=10; bottom1=8 bottom2=4 bottom3=18)
答案64(top1=16 top2=12 top3=2; middle1=8 middle2=4 middle3=18; bottom1=6 bottom2=14 bottom3=10)
答案65(top1=18 top2=2 top3=10; middle1=4 middle2=12 middle3=14; bottom1=8 bottom2=16 bottom3=6)
答案66(top1=18 top2=2 top3=10; middle1=8 middle2=16 middle3=6; bottom1=4 bottom2=12 bottom3=14)
答案67(top1=18 top2=4 top3=8; middle1=2 middle2=12 middle3=16; bottom1=10 bottom2=14 bottom3=6)
答案68(top1=18 top2=4 top3=8; middle1=10 middle2=14 middle3=6; bottom1=2 bottom2=12 bottom3=16)
答案69(top1=18 top2=8 top3=4; middle1=2 middle2=16 middle3=12; bottom1=10 bottom2=6 bottom3=14)
答案70(top1=18 top2=8 top3=4; middle1=10 middle2=6 middle3=14; bottom1=2 bottom2=16 bottom3=12)
答案71(top1=18 top2=10 top3=2; middle1=4 middle2=14 middle3=12; bottom1=8 bottom2=6 bottom3=16)
答案72(top1=18 top2=10 top3=2; middle1=8 middle2=6 middle3=16; bottom1=4 bottom2=14 bottom3=12)
55555555555!有太多的解了!如果把每行每列相加都等于30,再加上对角线3个数和也为30的话,就少很多了,呵呵。。。
答案1(top1=4 top2=14 top3=12; middle1=18 middle2=10 middle3=2; bottom1=8 bottom2=6 bottom3=16)
答案2(top1=4 top2=18 top3=8; middle1=14 middle2=10 middle3=6; bottom1=12 bottom2=2 bottom3=16)
答案3(top1=8 top2=6 top3=16; middle1=18 middle2=10 middle3=2; bottom1=4 bottom2=14 bottom3=12)
答案4(top1=8 top2=18 top3=4; middle1=6 middle2=10 middle3=14; bottom1=16 bottom2=2 bottom3=12)
答案5(top1=12 top2=2 top3=16; middle1=14 middle2=10 middle3=6; bottom1=4 bottom2=18 bottom3=8)
答案6(top1=12 top2=14 top3=4; middle1=2 middle2=10 middle3=18; bottom1=16 bottom2=6 bottom3=8)
答案7(top1=16 top2=2 top3=12; middle1=6 middle2=10 middle3=14; bottom1=8 bottom2=18 bottom3=4)
答案8(top1=16 top2=6 top3=8; middle1=2 middle2=10 middle3=18; bottom1=12 bottom2=14 bottom3=4)
弄了一个下午,郁闷...^_^...
 
顶一下,是穷举吗
 
有高手给提供一段代码吗、
 
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
就可以了.
^^
 
其实这就是一个排列的问题。
先从10个数中取出9个数来排序在[A0]..A[8]。
最后把
A[0]+A[1]+aA[2]=30 and
A[3]+A[4]+aA[5]=30 and
A[6]+A[7]+aA[8]=30 and
.....
的列出来
我写了一个函数
aLoc:array of string;

procedure PMN(SLst:Tstrings;RangC,n:integer);
//参数说明:SLst:要排列的列表,可以用一个memo作为输入界面,比如(2,4,...18],RangC,n都是要取的数量,比如此例为9
var
tLst:tstrings;
ic,jc:integer;
vStr:string;
begin
if n<=SLst.Count then
try
tLst:=tstringlist.Create;
for ic:=0 to SLst.Count-1 do
begin
tLst.Assign(SLst);
ALoc[RangC-n]:=tLst[ic];
tLst.Delete(ic);
if n=1 then
begin
Vstr:='';
for jc:=0 to Rangc-1 do
if Vstr='' then
Vstr:=ALoc[jc]
else
Vstr:=VStr+'--'+ALoc[jc];
form1.Listbox1.Items.Add(Vstr);//把各种排列列出来
//特别针对此例而加即把30的列出来
if Rangc=9 then
if strtoint(ALoc[0])+strtoint(ALoc[1])+strtoint(ALoc[2])=30 and
......
then
form1.Listbox2.Items.Add(Vstr);

end
else
PMN(tLst,RangC,n-1);//递归调用
end;
finally
tlst.Free;
end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var
vStrLst:Tstrings;
begin
if trim(edit13.Text)<>'' then
try
vStrLst:=Tstringlist.Create;
vStrLst.Assign(memo2.Lines);
Listbox1.Items.Clear;
SetLength(ALoc,strtoint(edit13.Text));
PMN(vStrLst,strtoint(edit13.Text),strtoint(edit13.Text));
finally
vStrLst.Free;
end;
edit12.Text:=inttostr(listbox1.Items.Count);
end;

把我简写的代码加上去,改一个符合自己界面的控件名运行即可。由于列出了所有的可能排列,所以内存要大一点,列出几百W可能的列要点时间
当然如果你仅仅是为了这个例子,那么优化一个。比如11,12,14,16,18不能放中间(自己想)即Aloc[4]不能等于他们一但等于不再本次递归调下而继续下一个排序。还有每三行之和不等于30时也可以跳个这一次排序等等那就快多了
 
后退
顶部