创建一个未知数量的循环 [英] Create an Unknown Number of Loops
问题描述
这是我生成
的简单代码所有可能的组合为
示例
1,2,3:
- 显示:
123
132
213
231
312
321
我想创建可变数量的for循环,让用户确定给定字符串的长度...
有没有人有想法...
提前感谢。
type
TNumber ='0'..'9';
procedure TForm1.Button1Click(Sender:TObject);
var
数字:数组[0..3];
a,b,c,d:整数;
s:string;
begin
数字[0]:='1';
数字[1]:='8';
数字[2]:='7';
数字[3]:='2';
a:=低(数字)到高(数字)do
b:=低(数字)到高(数字)do
for c:= low(Numbers)to High (数字)对于d:=低(数字)到高(数字)做
do
开始
s:=数字[a] +数字[b] +数字[c] +数字[ d];
如果
(发生('1',s)> 1)或
(发生('8',s)> 1)或
(发生('7 ',s)> 1)或
(发生('2',s)> 1)
然后
继续
else
Memo1.Lines.Add (s);
结束
结束
函数TForm1.Occurrences(const Substring,Text:string):Integer;
var
偏移量:整数;
begin
结果:= 0;
Offset:= PosEx(Substring,Text,1);
,而Offset<> 0 do
begin
Inc(Result);
偏移量:= PosEx(Substring,Text,offset + length(Substring));
结束
结束
结束。
这是一些生成你想要的输出的代码。你需要为你的需要一点点工作,但是在这个递归解决方案中表达的概念是重要的:
程序泄漏;
{$ APPTYPE CONSOLE}
type
TElements ='1'..'3';
procedure EnumerateCombinations(const Stem:string; Len:Integer);
var
i:整数;
el:TElements;
二手:一组TElements;
begin
如果Len = 0,则
exit;
已使用:= [];
for i:= 1 to Length(Stem)do
Include(Used,Stem [i]);
for el:= low(el)to high(el)do
begin
如果el在使用然后
继续;
如果Len = 1然后
Writeln(Stem + el)
else
枚举组合(Stem + el,Len-1)
end;
结束
程序主;
begin
EnumerateCombinations('',1 + ord(high(TElements)) - ord(low(TElements)));
结束
开始
主;
Readln;
结束。
输出:
123
/ pre>
132
213
231
312
321
如果您更改
TElements
的定义,例如更改为'1' ..'4'
然后你会看到24个可能的排列。this is my simple code to generate all possible combinations of a set for example
1,2,3:
- Display: 123 132 213 231 312 321
i want to create variable number of for loops to let the user determine the length of given string...
does anyone have an idea...
thank's in advance.
type
TNumber = '0'..'9';
procedure TForm1.Button1Click(Sender: TObject);
var
Numbers: array[0..3] of TNumber;
a, b, c, d: Integer;
s: string;
begin
Numbers[0] := '1';
Numbers[1] := '8';
Numbers[2] := '7';
Numbers[3] := '2';
for a := low(Numbers) to High(Numbers) do
for b := low(Numbers) to High(Numbers) do
for c := low(Numbers) to High(Numbers) do
for d := low(Numbers) to High(Numbers) do
begin
s := Numbers[a] + Numbers[b] + Numbers[c] + Numbers[d];
if
(Occurrences('1', s) > 1 ) or
(Occurrences('8', s) > 1 ) or
(Occurrences('7', s) > 1 ) or
(Occurrences('2', s) > 1 )
then
Continue
else
Memo1.Lines.Add(s);
end;
end;
function TForm1.Occurrences(const Substring, Text: string): Integer;
var
Offset: Integer;
begin
Result := 0;
Offset := PosEx(Substring, Text, 1);
while Offset <> 0 do
begin
Inc(Result);
Offset := PosEx(Substring, Text, offset + length(Substring));
end;
end;
end.
Here is some code that produces the output you desire. You'd need to work it around a bit for your needs, but the concept expressed in this recursive solution is the important thing:
program Permuatations;
{$APPTYPE CONSOLE}
type
TElements = '1'..'3';
procedure EnumerateCombinations(const Stem: string; Len: Integer);
var
i: Integer;
el: TElements;
Used: set of TElements;
begin
if Len=0 then
exit;
Used := [];
for i := 1 to Length(Stem) do
Include(Used, Stem[i]);
for el := low(el) to high(el) do
begin
if el in Used then
continue;
if Len=1 then
Writeln(Stem+el)
else
EnumerateCombinations(Stem+el, Len-1)
end;
end;
procedure Main;
begin
EnumerateCombinations('', 1+ord(high(TElements))-ord(low(TElements)));
end;
begin
Main;
Readln;
end.
Output:
123
132
213
231
312
321
If you change the definition of TElements
, for example to '1'..'4'
then you will see the 24 possible permutations.
这篇关于创建一个未知数量的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!