#include <iostream>
using namespace std;
int NumberOfPartitions(int array[], int length, int target);
int main()
{
int sampleSet[] = {1, 3, 4, 5};
int sz = sizeof(sampleSet) / sizeof(int);
cout << NumberOfPartitions(sampleSet, sz, 5) << endl
<< NumberOfPartitions(sampleSet, sz, 11) << endl;
return 0;
}
int NumberOfPartitions(int array[], int length, int target)
{
// simple case
if (length == 0) {
if (target == 0) return 1;
else return 0;
}
return NumberOfPartitions(array, length-1, target-array[length-1]) +
NumberOfPartitions(array, length-1, target);
}
recursion demo(problem from Stanford cs106b course reader)
最新推荐文章于 2019-07-15 15:59:59 发布