2013-05-05 97 views
-1

我有結構查找n個k個元素的所有組合

typedef struct st1 
{ 
    double start; 
    double step; 
    double stop; 
} ST; 

我需要找到那些優化參數,從開始將停止在步驟步驟的所有的組合更多鈔票的數組。

例如:

array[0].start = 0 
array[0].step = 1 
array[0].stop = 2 

array[1].start = 5 
array[1].step = 1 
array[1].stop = 6 

的組合將是:

0,5 0,6 1,5 1,6 2,5 2,6

¿我如何能實現這是一個N大小的C選項數組?

for(i=0; i<n; i++){ 
    .... 
} 
+0

嗯......我已經解決了類似的問題,具有固定數組大小時嵌套循環。問題是數組大小是可變的。我不是在尋找一個實現,而是如何實現它的線索。無論如何感謝您的評論。我希望我還在做功課,但那是很久以前;) – Jorge 2013-05-05 16:57:32

+0

[算法從n返回k元素的所有組合]可能的重複(http://stackoverflow.com/questions/127704/algorithm-to-return- k-elements-from-n的所有組合) – 2013-05-05 17:04:52

+0

@JimMischel:可能是重複的,但不是該帖子。這一個似乎是關於排列而不是組合,OP的標題和令人費解的術語使用不當。 – 2013-05-05 17:18:43

回答

0

一些僞代碼(實際上Kalkulon代碼),它可以簡單地翻譯成C代碼:

::array={{0,1,2},{5,1,6}}, 
::N=Size(array), 
::combination={0,0}, 

comb()= 
(
n=0, 
do(
    finishCtr = 0, 
    i = n, 
    for(j = 0; j < N; j++; // do 
    maxsteps=(array[j,2] - array[j,0])/array[j,1] + 1, 
    steps = i % maxsteps, // modulo 
    i = ip(i/maxsteps), // integer division 
    elem = array[j,0] + steps * array[j,1], 
    if(i > 0 && steps == 0; finishCtr++), 
    combination[j] = elem 
), 
    // print result if not already all combinations are done 
    if(finishCtr < N; PrintLn((string)combination)), 
    n++ 
; // do-while 
finishCtr < N 
) 
); 

輸出:

{0, 5} 
{1, 5} 
{2, 5} 
{0, 6} 
{1, 6} 
{2, 6} 
相關問題