如何在delphi中跳过一些嵌套循环?

时间:2017-10-04 20:26:30

标签: delphi nested-loops skip

有人可以在Delphi中帮助我这个主题吗?

我有5个嵌套" for"块循环用变量k1,k2 ... k5迭代 另一个变量k必须控制5个以上循环的深度。此变量在运行时定义。

是否可以消除k内部的执行情况'好像他们被评论过一样?解释代码的简短列表如下:

k := 2;
for k1 := 1 to 100 do begin  
   for k2 := 1 to 100 do begin  
      for k3 := 1 to 100 do begin  
         for k4 := 1 to 100 do begin {want to skip this loop}
            for k5 := 1 to 100 do begin {want to skip this loop}  
               AnywayDoThat; // must execute anyway  
            end; {k5}  
         end; {k4}  
      end;  
   end;
end;  

进一步解释,当k等于2时,我希望避免执行循环k4和k5(以大括号结尾的代码行),缩短程序的完整流程。当然,proc AnywayDoThat必须执行。

我使用Delphi 6。

修改:对不好的措辞'淘汰'抱歉。目的是避免不必要的迭代。通过使其仅执行一次来跳过循环的技巧是完美的。因此,proc AnywayDoThat只根据k1,k2,k3到达。

2 个答案:

答案 0 :(得分:1)

另一种方法是使用递归。这是一个相当简化的示例,基于您的代码将当前索引写入文本文件(抱歉,没有Delphi 6进行测试):

var
  F: Textfile;

procedure AnywayDoThat(const Index: array of Integer);
var
  I: Integer;
begin
  for I := Low(Index) to High(Index) do
    Write(F, Index[I], '.');
  Writeln(F);
end;

procedure Loop(Level, MaxLevel: Integer; const Index: array of Integer);
var
  arr: array of Integer;
  I: Integer;
  L: Integer;
begin
  if Level < MaxLevel then begin
    L := Length(Index);
    SetLength(arr, L + 1);
    for I := 0 to L - 1 do begin
      arr[I] := Index[I];
    end;
    for I := 1 to 100 do begin
      arr[L] := I;
      Loop(Level + 1, MaxLevel, arr);
    end;
  end
  else begin
    AnywayDoThat(Index);
  end;
end;

procedure Main();
var
  k: Integer;
  I: Integer;
  Index: array of Integer;
begin
  AssignFile(F, 'c:\Temp\test.txt');
  Rewrite(F);
  k := 2;
  SetLength(Index, 1);
  for I := 1 to 100 do begin
    Index[0] := I;
    Loop(1, 5 - k, Index);
  end;
  Close(F);
end;

答案 1 :(得分:0)

您可以使用以下技巧

Var 
  Maxes: array [1..5] of integer;
  Eliminators: array [1..5] of integer; // they have to be of same length
begin
  SetArray(Maxes, [100, 100, 100, 100, 100]);
  SetArray(Eliminators, [1, 1, 1, 1, 1]);

  // Here you set K
  K := 2;  // you need to make sure K <= 5 

  // Set the Eliminators array 

  for I := 1 to K do
    begin
      Eliminators[5-I+1] := Maxes[5-I+1];
    end;

  // Now to the loops

  for k1 := 1 to Maxes[1] div Eliminators[1] do begin  
    for k2 := 1 to Maxes[2] div Eliminators[2] do begin  
      for k3 := 1 to Maxes[3] div Eliminators[3] do begin  
        for k4 := 1 to Maxes[4] div Eliminators[4] do begin 
          for k5 := 1 to Maxes[5] div Eliminators[5] do begin  
            AnywayDoThat; // must execute anyway  
          end;  
        end;   
      end;  
    end;
  end;  


end;

这里的主要思想是通过使循环执行一次来跳过循环。这可以进一步修改,使其跳过任何循环。