我试图在bruteforce中为下面的分区问题做伪代码.
一组整数X和一个整数k(k> 1).找到X的k个子集,使得每个子集中的数字总和为相同的量,并且没有两个子集具有共同的元素,或者得出结论:不存在这样的k个子集.问题是NP-Complete
例如,当X = {2,5,4,9,1,7,6,8}和k = 3时,可能的解决方案是:{2,5,7},{4,9,1}, {6,8}因为所有这些总计达到14.
对于穷举搜索我知道通常我们必须搜索每个可能的解决方案,看看目标是否相似.但由于这是分区问题,这可能很棘手.
算法暴力:
Subset= X.sum/K //I had a guess this would make the parition
For int i==1; I <subset; i++ // this would change partition if not found in the first one
If (j=0; I<n; i++)
Sum == s[i]
If sum == target
Display “found”
Else
“not found”
Run Code Online (Sandbox Code Playgroud)