作者:双鱼獒主 | 来源:互联网 | 2023-05-19 16:05
小弟昨天帮人问问题,可能发了个类似考题的东东,结果被老大给扣了,心想这道题用Delphi怎么来写,输入一个数,比如输入3,那么就是求3位数的任意排列组合,应该是10*10*101000个数,怎么写
小弟昨天帮人问问题,可能发了个类似考题的东东,结果被老大给扣了,心想这道题用 Delphi怎么来写,输入一个数,比如输入3,那么就是求3位数的任意排列组合,应该是10*10*10=1000个数,怎么写这个算法,并且把所有的结果显示出来,再说了N是变量
12 个解决方案
列出从0-10^n-1的所有自然数
这也叫算法么?
一个取巧的办法
uses math;
procedure TForm1.Button1Click(Sender: TObject);
var n,i:integer;
m:string;
begin
n:=3;
m:=IntToStr(n);
for i:=0 to Trunc(Power(10,n))-1 do
Memo1.Lines.Add(Format('%'+m+'.'+m+'d',[i]));
end;
liangqingzhi(老之)
能给小弟解释一下吗?看不太懂,谢谢
没有环境,大概写了个思路,可以参考参考
const
BasicNumber :Array [0..9] of Integer = [0,1,2,3,4,5,6,7,8,9];
procedure Parade(num :Integer; list :var TStringList);
var
index, sindex :Integer;
slist :TStringList;
begin
slist := TStringList.Create();
try
if (num < 1) then
begin
//do nothing
end else
begin
for index := 0 to 9 do
begin
for sindex := 0 to list.Count-1 do
begin
slist.add(list.Strings[sindex]+IntToStr(BasicNumer[index]));
end;
end;
Parade(num-1, slist);
end;
if (slist.Count > 0) begin
list.Clear();
list.Text := slist.Text;
end;
finally
slist.FreeAndNil();
end;
end;
//test
procedure TForm1.Button1Click(Sender: TObject);
var
num, index :Integer;
list :TStringList;
begin
list := TStringList.Create();
try
//get input number
try
num := StrToInt(Exit1.Text);
except
num := 3; //default value
end;
//parade
Parade(num, list);
//show result
for index := 0 to list.Count-1 do
begin
Memo1.Lines.Add(list.Strings[index]);
end;
finally
list.FreeAndNil();
end;
end;
疏忽了个地方
procedure Parade(num :Integer; list :var TStringList);
var
index, sindex :Integer;
slist :TStringList;
begin
slist := TStringList.Create();
try
if (num < 1) then
begin
//do nothing
end else
begin
if (list.Count > 0) then
begin
for index := 0 to 9 do
begin
slist.add(IntToStr(BasicNumer[index]));
end;
end else
begin
for index := 0 to 9 do
begin
for sindex := 0 to list.Count-1 do
begin
slist.add(list.Strings[sindex]+IntToStr(BasicNumer[index]));
end;
end;
end;
Parade(num-1, slist);
end;
if (slist.Count > 0) begin
list.Clear();
list.Text := slist.Text;
end;
finally
slist.FreeAndNil();
end;
end;
如果是全部枚举的话不就是。。。。
for i:=0 to 999 do
.....
前面用case k of
1: ..../9
2:...../99
......
找了台有delphi的机器测试了一下,把排列和组合都做了
关键是Parade和Combine函数,不是很复杂
以下是我测试的source
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
BASIC_NUM :Array [0..9] of String = ('0','1','2','3','4','5','6','7','8','9');
//BASIC_OBJ :Array [0..4] of String = ('Red', 'Green', 'Blue', 'White', 'Black');
AMOUNT_NUM :Integer = 10;
//AMOUNT_OBJ :Integer = 5;
PARADE_TYPE :Integer = 0;
COMBINE_TYPE :Integer = 1;
//排列(可以出现重复元素)
procedure Parade(nBase :Integer; Var sList :TStringList); //关键处理
//组合(不出现重复元素)
procedure Combine(nBase :Integer; Var sList :TStringList); //关键处理
var
MainForm: TMainForm;
nProcessType :Integer;
implementation
{$R *.dfm}
procedure Parade(nBase :Integer; Var sList :TStringList);
var
ssList :TStringList;
nIdx, nSidx :Integer;
begin
ssList := TStringList.Create;
try
if (nBase < 1) then
begin
//do nothing
end else
begin
if (sList.Count = 0) then
begin
for nIdx := 0 to AMOUNT_NUM-1 do
ssList.Add(BASIC_NUM[nIdx]);
end else
begin
for nIdx := 0 to AMOUNT_NUM-1 do
for nSidx := 0 to sList.Count-1 do
ssList.Add(BASIC_NUM[nIdx]+sList.Strings[nSidx]);
end;
Parade(nBase-1, ssList);
end;
if ssList.Count > 0 then
begin
sList.Clear;
sList.Text := ssList.Text;
end;
finally
ssList.Free;
end;
end;
procedure Combine(nBase :Integer; Var sList :TStringList);
var
ssList :TStringList;
nIdx, nSidx :Integer;
begin
ssList := TStringList.Create;
try
if (nBase < 1) then
begin
//do nothing
end else
begin
if (sList.Count = 0) then
begin
//for nIdx := 0 to AMOUNT_OBJ-1 do
for nIdx := 0 to AMOUNT_NUM-1 do
//ssList.Add(BASIC_OBJ[nIdx]);
ssList.Add(BASIC_NUM[nIdx]);
end else
begin
//for nIdx := 0 to AMOUNT_OBJ-1 do
for nIdx := 0 to AMOUNT_NUM-1 do
for nSidx := 0 to sList.Count-1 do
//if (Pos(BASIC_OBJ[nIdx], sList.Strings[nSidx]) = 0) then
if (Pos(BASIC_NUM[nIdx], sList.Strings[nSidx]) = 0) then
//ssList.Add(BASIC_OBJ[nIdx]+','+sList.Strings[nSidx]);
ssList.Add(BASIC_NUM[nIdx]+sList.Strings[nSidx]);
end;
Combine(nBase-1, ssList);
end;
if ssList.Count > 0 then
begin
sList.Clear;
sList.Text := ssList.Text;
end;
finally
ssList.Free;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Edit1.Text := '3';
Memo1.Lines.Clear;
RadioGroup1.ItemIndex := 0;
nProcessType := 0;
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
sBase :String;
nBase :Integer;
sList :TStringList;
begin
sBase := Trim(Edit1.Text);
if sBase = '' then
begin
ShowMessage('Please input a basic number');
Edit1.SetFocus;
exit;
end;
try
nBase := StrToInt(sBase);
except
nBase := 0;
end;
Memo1.Lines.Clear;
nProcessType := RadioGroup1.ItemIndex;
sList := TStringList.Create;
try
if nProcessType = PARADE_TYPE then
Parade(nBase, sList)
else
Combine(nBase, sList);
Memo1.Lines.Text := sList.Text;
Memo1.Lines.Add('Total: '+FloatToStr(Memo1.Lines.Count));
finally
sList.Free;
end;
end;
end.