请教各位!(求教了我真的想不出来!) (100分)

  • 主题发起人 主题发起人 emailcdz
  • 开始时间 开始时间
E

emailcdz

Unregistered / Unconfirmed
GUEST, unregistred user!
我怎样把
a[1,1] a[1,2] a[1,3]
a[2,1] a[2,2] a[2,3]
....................
a[100,1] a[100,2] a[100,3]
[h3]的每一行中均只取一个数的排列(共有3^100次方种)[/h3]
都写出来!求教了我真的想不出来!
 
打印出这些内容,还是打印出这个数组里面的值排列:
[1,1] a[1,2] a[1,3]
a[2,1] a[2,2] a[2,3]
....................
a[100,1] a[100,2] a[100,3]
?
 
for i := 1 to 100do
for j := 1 to 3 do
a[i,j]:=???
 
用2个循环就搞定
for i:=1 to 100do
for j:=1 to 100do
a[i,j]:=
不就OK了,绝对没有重复
 
这个排列有3^100次方种,上面的各位好像不对!
 
我看是你问题没有说完整把!!!
 
张无忌的方法也应该可以吧,求数组这个是一个最基本也是最简单的方法啊
 
procedure TForm1.getitemNo(ic : integer);
begin
if c[ic] = 4 then
begin
c[ic] := 1;
c[ic - 1] := c[ic - 1] + 1;
if c[ic - 1] = 4 then
getitemno(ic - 1);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
...
begin
b := '';
for i := 1 to 100do
b := b + a[i, c];
//out first strings
while c[1] < 4do
begin
k := k + 1;
b := '';
c[100] := c[100] + 1;
if c[100] = 4 then
getitemno(100);
for i := 1 to 100do
b := b + a[i, c];
//out b
end;
end;
 
补充:
>>...只取一个数的排列...
如果对b还要做一次排列,那我的机器要吃不消,你自己干吧!用递归可以实现。
解决的思路是100位的4进制算法,初始值为 111111...1,然后对其进行加1操作,直到溢出。
 
sorry, 有错误!
procedure TForm1.getitemNo(ic : integer);
begin
if c[ic] = 4 then
<---改成 if ((c[ic] = 4) and (ic <> 1)) then
begin
c[ic] := 1;
c[ic - 1] := c[ic - 1] + 1;
if c[ic - 1] = 4 then
getitemno(ic - 1);
end;
end;
 
楼上的方案已经很好了。我这里还有一个更加通用的,请看:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=999248 (列出足彩的所有单注)
 
如果是要列出这些数的组合,则可用100次1-3的循环实现:
for i1:=1 to 3do
for i2:=1 to 3do
for i3:=1 to 3do
...
for i100:=1 to 3do
print(....);
正好3^100
如果是这些数的组合,则还需乘上100个数的排列值。
 
楼上的方案:
>>用100次1-3的循环实现, [:D]太多了!
这儿的高手太多了!我再看看就散分!
好好学习,天天向上!
 
可以用递归
src_array[i,j] //源二维数组
max_row=100;
max_column=3;
tag[max_row];
//用来标志第 i (1到100)行选了第j(1到3)个值


procedure get_value(last_row:integer) ;
var this_row,i,j:integer;
begin
if last_row>=max_row then
//到了最后一行
begin
writeln("程序结束");
end
else
//否则
begin
this_row:=last_row+1;
if tag[this_row]>=3 then
//如果本行选的是第三个值的话,到下一行改选
get_value(this_row)
else
begin
tag[this_row]:=tag[this_row]+1;
//选三个值中的下一个
for i:=1 to last_rowdo
//把前面的值全清为1
tag:=1;
for i:=1 to max_rowdo
writeln(src_array[i,tag]); //本次排列输出
get_value(0);
end;

end;

end;


在主程中调用 get_value(0)即可
程序我没有测试,你可以试试看
 
多人接受答案了。
 
后退
顶部