2014-12-05 98 views

回答

0

的一種方式,以提高在蠻力方法:

s = the sum of all the numbers 
for i = 2 to 10 
    k = s/i 

    if k is an integer then 
    Get all partitions of the input array with a minimum subset size of 2 elements and a maximum of n-1 elements. 
    Check each partition as it's generated to see if all the subsets have the same sum. 
    end if 

    next i 

硬(和慢)部分產生的分區。你可以用遞歸函數來做到這一點。劃分20個數字的速度不應該不合理。您可以通過儘早拋出不等額子集分區來優化分區生成。

+0

能否請你解釋一下「如果k是一個整數,那麼看看你是否可以讓我的子集,每個加起來k」。我的意思是,如何找到覆蓋所有n個數字並且每個都具有相等總和的不相交子集? – 2014-12-06 04:55:11

+0

這是蠻力的一部分,但是因爲你只需要做幾個值,所以速度要快得多。我會在答案中解釋。 – xpda 2014-12-06 06:07:35

+0

好的..等待解釋。其實我很難實現我必須找到我等分的不相交子集的部分。 – 2014-12-06 06:42:01

相關問題