recursion demo(problem from Stanford cs106b course reader)

来源:互联网 发布:淘宝助理是做什么工作 编辑:程序博客网 时间:2024/05/22 07:16
#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 caseif (length == 0) {if (target == 0) return 1;else return 0;}return NumberOfPartitions(array, length-1, target-array[length-1]) +   NumberOfPartitions(array, length-1, target);}

0 0
原创粉丝点击