这是一个基本的动态编程问题 - 得分组合数 . 我知道这个问题的自下而上的方法很有效 .

但是,我无法为此问题寻找自上而下的解决方案方法 . 缓存递归部分给了我们超过必要的组合(其中排序/分数序列也是一个因素,因此,为了避免它,我们需要提供一个约束来使序列单调增加 . 这是相同的递归方法.Dynamic Programming - Number of distinct combinations to reach a given score

这是我目前的代码:

#include <iostream>
#include <vector>
using namespace std;

int helper(int target, vector<int>& coins, vector<int>& cache, int min) {
    if(target < 0) return 0;
    if(target == 0) return 1;
    if(cache[target] != 0) return cache[target];

    for(auto& c : coins) {
        if(target >= c && min <= c) {
            //cout << min << " " << c << " " << target << endl;
            cache[target] += helper(target-c, coins, cache, c) ;
            //cout << cache[target] << endl;            
        }

    }

    return cache[target];

}


int main() {

    vector<int> coins{2, 3};

    int target = 7;
    vector<int> cache(target+1, 0);
    cache[0] = 1;
    cache [7] = helper(target, coins, cache, 1);

    for (auto& x : cache) cout << x << endl;
    return 0;
}

Here是可运行的ideone链接 .