dongqi6486 2010-01-16 23:02
浏览 38
已采纳

所有阵列的可能性

I have pascal code (programming language actually doesn't mean anything):

box[1] := 14;
box[2] := 2;
box[3] := 4;
box[4] := 5;
box[5] := 6;
box[6] := 8;

I want to get all possibilities. For instance, box[1] = box[6], then box[6] = box[1]. Yes, I can write it by my hand, but I guess I can make it more clever, by loop. Any suggestions?

  • 写回答

3条回答 默认 最新

  • dongwu9170 2010-01-17 10:15
    关注

    I have taken the first permutation algorithm I found in wikipedia and implemented it in Delphi (2009); I hope that is what you are looking for:

    type
      TIntegerArray = array of Integer;
    
    procedure Permutation(K: Integer; var A: TIntegerArray);
    var
      I, J: Integer;
      Tmp: Integer;
    
    begin
      for I:= 2 to Length(A) do begin
        J:= K mod I;
        Tmp:= A[J];
        A[J]:= A[I - 1];
        A[I - 1]:= Tmp;
        K:= K div I;
       end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      K, I: Integer;
      A: TIntegerArray;
      S: string;
    
    begin
      Memo1.Lines.Clear;
      for K:= 0 to 719 do begin
        A:= TIntegerArray.Create(14, 2, 4, 5, 6, 8);
        Permutation(K, A);
        S:= '';
        for I:= 0 to Length(A) - 1 do
          S:= S + Format('%3.d ', [A[I]]);
        Memo1.Lines.Add(S);
      end;
    end;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能