全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1006
推到 Plurk!
推到 Facebook!

請問如何列出所有組合

尚未結案
shing.net
中階會員


發表:207
回覆:124
積分:66
註冊:2002-03-16

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-10 13:29:26 IP:61.218.xxx.xxx 未訂閱
若有一陣列為a[]={1 2 3 4} 若輸入b=1 其組合有4*1=4種組合 若輸入b=2 其組合有4*4=16種組合,請問如何撰寫程式將其組合全部列出來至c[]中 {1 1 1 1} {1 1 1 2} . . . {4 4 4 4}
shing.net
中階會員


發表:207
回覆:124
積分:66
註冊:2002-03-16

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-10 13:56:18 IP:61.218.xxx.xxx 未訂閱
對不起,寫錯了 b=2 應該是 {1 1} {1 2} {1 3}   .   .   . {4 4} 共16種組合    
引言: 若有一陣列為a[]={1 2 3 4} 若輸入b=1 其組合有4*1=4種組合 若輸入b=2 其組合有4*4=16種組合,請問如何撰寫程式將其組合全部列出來至c[]中 {1 1 1 1} {1 1 1 2} . . . {4 4 4 4}
mine
中階會員


發表:28
回覆:129
積分:56
註冊:2004-03-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-10 15:17:59 IP:61.221.xxx.xxx 未訂閱
hihi 不知合不合您用
procedure TForm1.Button1Click(Sender: TObject);
var
ta:array[0..3]of byte;
j,b,i:integer;
begin
b:=2;
for i:=0 to 3 do begin
 ta[i]:=i 1;
end;
if b>=2 then begin
Repeat
for i:=0 to 3 do begin
 for j:=0 to 3 do begin
 memo1.text:=memo1.text inttostr(ta[i]) inttostr(ta[j]) ' ';
 end;
 end;
 b:=b-1;
until b<=1 ;
end;
有些地方可能要改:P 搞不懂!搞不懂!永遠都搞不懂!!
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-10 18:33:10 IP:220.130.xxx.xxx 未訂閱
procedure TForm1.Button1Click(Sender: TObject);
const
  A: array[0..3] of Integer = (1,2,3,4); Length(A)進位系統      function IntToStr_YouWant(Value: Integer): string;
  var
    I: Integer;
  begin
    Result := '';
    I := Length(A);
    repeat
      Result := IntToStr(A[Value mod Length(A)])   ','   Result;
      Value := Value div Length(A);
      Dec(I);
    until I = 0;        Result := '{'   Result;
    Result[Length(Result)] := '}';      end;      function GetResultList(ElementCount, ElementPerSet: Integer):  
TStringList;
  var
    I, Count: Integer;
  begin
    Result := TStringList.Create;
    if ElementPerSet > ElementCount then Exit;
    if(ElementCount > 0)and(ElementPerSet > 0) then
    begin
      Count := Round(IntPower(ElementCount,ElementPerSet));
      for I := 0 to Count-1 do
              Result.Add(IntToStr_YouWant(I));
    end;
  end;    var
  B: Integer;
  L: TStringList;
begin
  ShowMessage(IntToStr(Length(A)));
  B := 4;
  L := GetResultList(Length(A),B);
  if Assigned(L) then
    try
      Label1.Caption := IntToStr(L.Count);
      ListBox1.Items.Text := L.Text;
    finally
      FreeAndNil(L);
    end;
end;    
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-10 18:43:28 IP:220.130.xxx.xxx 未訂閱
修正!  
  function IntToStr_YouWant(Value, ElementPerSet: Integer): string;<--傳入數字個數
  var
    I: Integer;
  begin
    Result := '';
    if ElementPerSet > 0 then
    begin
      I := ElementPerSet;<--限制數字個數
      repeat
        Result := IntToStr(A[Value mod Length(A)])   ','   Result;
        Value := Value div Length(A);
        Dec(I);
      until I = 0;
      Result := '{'   Result;
      Result[Length(Result)] := '}';
    end;
  end;      function GetResultList(ElementCount, ElementPerSet: Integer): TStringList;
  var
    I, Count: Integer;
  begin
    Result := TStringList.Create;
    if ElementPerSet > ElementCount then Exit;
    if(ElementCount > 0)and(ElementPerSet > 0) then
    begin
      Count := Round(IntPower(ElementCount,ElementPerSet));
      for I := 0 to Count-1 do
              Result.Add(IntToStr_YouWant(I, ElementPerSet));<--傳入數字個數
    end;
  end;
 
系統時間:2024-07-03 18:26:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!