这不是家庭作业,我没有钱上学,所以我一边在高速公路上的收费站轮班工作,一边自学(长夜,顾客很少)。
我正在尝试实现一个简单的子集求和算法,给定一个整数数组,返回它的子集,其总和等于所需的总和,报告找到它需要多少次调用。
我使用 Collections 在 Java 中做了一个实现,但那是非常臃肿的代码,即使我能够返回所有集合加起来达到所需的数字以及告诉函数在第一次匹配时停止或不停止。
我对这段代码的问题如下:而不是在 2^n 时间内运行(当没有找到结果时,这对于这样的实现是正确的,不是吗?)它在 [2^(n+1)]- 中运行1次; O(2^n) 正如评论所指出的那样。我可以明白为什么我检查 (runningTotal == targetTotal) 比我能做的更深层次,本质上是我自己增加了额外的深度,不是吗?我试图尽可能干净地模拟基本案例,如果您检测到任何“代码气味”,请告诉我。我一看到(runningTotal + 考虑)== targetTotal 就应该打破吗?
注意:我认为这不属于“代码审查”,因为我询问的是特定代码行,而不是整体方法(如果我需要更改方法,就这样吧,我这样做是为了学习)。
这是我的尝试(除了上面提到的缺乏优化之外,这是“可以通过”的 C/C++ 吗?):
#include <iostream>
using namespace std;
bool setTotalling(int chooseFrom[], int nChoices, int targetTotal,
int chooseIndex, int runningTotal, int solutionSet[], int &solutionDigits,
int &nIterations) {
nIterations++;
if (runningTotal == targetTotal) {
return true;
}
if (chooseIndex >= nChoices) {
return false;
}
int consider = chooseFrom[chooseIndex];
if (setTotalling(chooseFrom, nChoices, targetTotal, chooseIndex + 1,
runningTotal + consider, solutionSet, solutionDigits, nIterations)) {
solutionSet[solutionDigits++] = consider;
return true;
}
if (setTotalling(chooseFrom, nChoices, targetTotal, chooseIndex + 1,
runningTotal, solutionSet, solutionDigits, nIterations)) {
return true;
}
return false;
}
void testSetTotalling() {
int chooseFrom[] = { 1, 2, 5, 9, 10 };
int nChoices = 5;
int targetTotal = 23;
int chooseIndex = 0;
int runningTotal = 0;
int solutionSet[] = { 0, 0, 0, 0, 0 };
int solutionDigits = 0;
int nIterations = 0;
cout << "Looking for a set of numbers totalling" << endl << "--> "
<< targetTotal << endl << "choosing from these:" << endl;
for (int i = 0; i < nChoices; i++) {
int n = chooseFrom[i];
cout << n << ", ";
}
cout << endl << endl;
bool setExists = setTotalling(chooseFrom, nChoices, targetTotal, chooseIndex,
runningTotal, solutionSet, solutionDigits, nIterations);
if (setExists) {
cout << "Found:" << endl;
for (int i = 0; i < solutionDigits; i++) {
int n = solutionSet[i];
cout << n << ", ";
}
cout << endl;
} else {
cout << "Not found." << endl;
}
cout << "Iterations: " << nIterations << endl;
}
int main() {
testSetTotalling();
return 0;
}