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

請教如何可以計算出Array的數目組合?

尚未結案
hanginlo
一般會員


發表:6
回覆:3
積分:1
註冊:2003-05-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-02-26 02:36:52 IP:203.218.xxx.xxx 未訂閱
每一行只取一個數目,要找出以下數目總和為210143的數字組合,想了很久,但都想不出答案,請問應該怎樣做呢? 謝謝! 行/欄 a b c d e f 1 0 905 1810 2715 3620 4525 2 0 1560 3120 4680 6240 7800 3 0 2940 5880 8820 11760 14700 4 0 2937.5 5875 8812.5 11750 14687.5 5 0 1185 2370 3555 4740 5925 6 0 8055 16110 24165 32220 40275 7 0 2835 5670 8505 11340 14175 8 0 2900 5800 8700 11600 14500 9 0 760 1520 2280 3040 3800 10 0 2904 5808 8712 11616 14520 11 0 3480 6960 10440 13920 17400 12 0 12240 24480 36720 48960 61200 13 0 3420 6840 10260 13680 17100 14 0 2890 5780 8670 11560 14450 15 0 2510 5020 7530 10040 12550 16 0 3520 7040 10560 14080 17600 17 0 5810 11620 17430 23240 29050 18 0 2875 5750 8625 11500 14375 19 0 3096 6192 9288 12384 15480 20 0 2730 5460 8190 10920 13650 21 0 2740 5480 8220 10960 13700 22 0 2688 5376 8064 10752 13440 23 0 2896 5792 8688 11584 14480 24 0 2700 5400 8100 10800 13500
boson
中階會員


發表:74
回覆:155
積分:85
註冊:2004-07-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-02-26 11:04:53 IP:218.170.xxx.xxx 未訂閱
 
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;    const
  Ary : array[1..24, 1..6] of integer = (
            ( 0,  905,      1810,    2715,     3620,     4525     ),
            ( 0,  1560,     3120,    4680,     6240,     7800     ),
            ( 0,  2940,     5880,    8820,     11760,    14700    ),
            ( 0,  0,        5875,    0,        11750,    0        ),
            ( 0,  1185,     2370,    3555,     4740,     5925     ),
            ( 0,  8055,     16110,   24165,    32220,    40275    ),
            ( 0,  2835,     5670,    8505,     11340,    14175    ),
            ( 0,  2900,     5800,    8700,     11600,    14500    ),
            ( 0,  760,      1520,    2280,     3040,     3800     ),
            ( 0,  2904,     5808,    8712,     11616,    14520    ),
            ( 0,  3480,     6960,    10440,    13920,    17400    ),
            ( 0,  12240,    24480,   36720,    48960,    61200    ),
            ( 0,  3420,     6840,    10260,    13680,    17100    ),
            ( 0,  2890,     5780,    8670,     11560,    14450    ),
            ( 0,  2510,     5020,    7530,     10040,    12550    ),
            ( 0,  3520,     7040,    10560,    14080,    17600    ),
            ( 0,  5810,     11620,   17430,    23240,    29050    ),
            ( 0,  2875,     5750,    8625,     11500,    14375    ),
            ( 0,  3096,     6192,    9288,     12384,    15480    ),
            ( 0,  2730,     5460,    8190,     10920,    13650    ),
            ( 0,  2740,     5480,    8220,     10960,    13700    ),
            ( 0,  2688,     5376,    8064,     10752,    13440    ),
            ( 0,  2896,     5792,    8688,     11584,    14480    ),
            ( 0,  2700,     5400,    8100,     10800,    13500    ));
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    StringGrid1: TStringGrid;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Answer : array[1..24] of integer;
    function Sum:integer;
    procedure OutputAnswer;
    function NextStep:boolean;
    function IncStep(Idx:integer):boolean;
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    function TForm1.Sum:integer;
var
   i:integer;   S:integer;
begin
   S:=0;
   for i:=1 to 24 do S:=S Ary[i,Answer[i]];
   Result:=S;
end;    procedure TForm1.OutputAnswer;
var
   i:integer;   Temp:string;
begin
   Temp:='';
   for i:=1 to 24 do Temp:=Temp '  ' IntToStr(Ary[i,Answer[i]]);
   ShowMessage('Answer : ' Temp);
end;    function TForm1.IncStep(Idx:integer):boolean;
begin
   if Idx > 0 then begin
      if Answer[Idx] < 24 then begin
         inc(Answer[Idx]);   Result:=True;
      end else begin
         Answer[Idx]:=1;
         Result:=IncStep(Idx-1);
      end;
   end else Result:=False;
end;    function TForm1.NextStep:boolean;
begin
   Result:=IncStep(24);
end;    procedure TForm1.Button1Click(Sender: TObject);
var
   i:integer;   Continue:boolean;
begin
   for i:=1 to 24 do Answer[i]:=1;
   repeat
      if Sum = 210143 then begin
         OutputAnswer;   Continue:=False;
      end else Continue:=NextStep;
   until (not Continue);
   ShowMessage('Done.');
end;    end.    
最簡單的做法, 也是最笨的做法, 就是一個一個數字去試 以上是採用遞迴的方式處理, 寫起來比較簡單一點, 但是效率很差 我沒有得出解答, 因為程式要跑很久很久 .... 直到海枯石爛 有興趣的話, 可以改寫成非遞迴的做法, 就可以"較快"得出解答, 雖說是"較快", 但你這輩子恐怕還是無法看到程式跑完 以上程式是假設所有的數字之間不具關聯性, 所以只能一個一個試 但事實上這些數字是有關聯性的, 要找出正確的解答, 可能要從思考關聯性著手 以上隨便寫寫, 若有謬誤之處, 請海涵 ! 發表人 - boson 於 2005/02/26 11:38:00
hanginlo
一般會員


發表:6
回覆:3
積分:1
註冊:2003-05-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-02-27 01:09:15 IP:203.218.xxx.xxx 未訂閱
謝謝您~
Brian77
中階會員


發表:8
回覆:114
積分:94
註冊:2002-05-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-02-27 22:16:40 IP:61.221.xxx.xxx 未訂閱
資料定義在陣列 Ary: array[0..23, 0..5] of double; 內
所求的和 = mAns:Double    var
  mMin[0..23]: array of double;
  mSet[0..23]: array of integer;
  i,j:integer; mSum:double;
begin
  Result:=False;
  //
  mMin[5]:=mAns;
  for i:=4 downto 0 do mMin[i]:=mMin[i 1]-Ary[i 1,5];
  // 起始值
  i:=0;
  mSum:=0;
  mSet[0]:=-1;
  //
  while true do
  begin
    // 找第 i 層的下一個選擇
    j:=mSet[i] 1;
    // 第 i 層已無可能解, 回上一層
    if (j>5) or ((mSum Ary[i,j])>mAns) then
    begin
      if i=0 then exit;
      dec(i); mSum:=mSum-Ary[i,mSet[i]];
      continue;
    end;
    // 記錄第 i 層
    mSet[i]:=j; mSum:=mSum Ary[i,j];
    // 已到最末層
    if i=23 then
    begin
      // 這個組合符合要求
      if mSum=mAns then
      begin
        Result:=True;
        exit;
      end;
      // 回上一層
      mSum:=mSum-Ary[i,j];
      dec(i); mSum:=mSum-Ary[i,mSet[i]];
      continue;
    end;
    // 找下一層的第一個可能解
    inc(i);
    j:=0;
    while (j<=5) and ((mSum Ary[i,j])5 then
    begin
      dec(i); mSum:=mSum-Ary[i,mSet[i]];
      dec(i); mSum:=mSum-Ary[i,mSet[i]];
    end else mSet[i]:=j-1;
  end;
end;
當它傳回 True 時表示有找到解, 解放在 mSet 陣列裡 它可在數毫秒內找到第一個解 但要找出所有解, 得很久... PS. 上例假設了各組數據已排序 (依所給的數據確實是由小排到大) 若各組數據並沒有都包含 0 的話, 那可再加上各階段最大值的比較 (仿照 mMin 製作 mMax) 如此可加速
系統時間:2024-06-24 2:59:09
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!