C++ C+中的记忆+;
根据板球队的得分,查找/打印获得得分的所有配置/方式。有3种方法可以获得2分、3分和7分 例如: 分数:10 输出: (0,1,1) (0,2,2) (0,0,5)C++ C+中的记忆+;,c++,memoization,C++,Memoization,根据板球队的得分,查找/打印获得得分的所有配置/方式。有3种方法可以获得2分、3分和7分 例如: 分数:10 输出: (0,1,1) (0,2,2) (0,0,5) void out(整数分、整数二、整数三、整数七) { 如果(分数=0) { cout为了避免重复,您需要进行排序,例如,如果您以前使用了分数3,则不允许使用分数7 void out(int score, int two, int three, int seven, int maxscore) { ... else
void out(整数分、整数二、整数三、整数七)
{
如果(分数=0)
{
cout为了避免重复,您需要进行排序,例如,如果您以前使用了分数3
,则不允许使用分数7
void out(int score, int two, int three, int seven, int maxscore)
{
...
else {
if (maxscore >= 7) output(score-7, two, three, seven+1, 7);
if (maxscore >= 3) output(score-3, two, three+1, seven, 3);
if (maxscore >= 2) output(score-2, two+1, three, seven, 2);
}
}
在这个问题中,使用记忆将更加复杂(甚至可能没有那么有用),因为您需要枚举所有解决方案(而不仅仅是计算它们)
记忆的想法是保留一个表,以避免重新计算相同的子问题。在这种情况下,子问题由分数和允许使用的最大分数定义,但是解决方案还需要考虑您已经使用了多少个2、3和7,如果您还将它们添加到键中,则每个键将仅为vi只定位过一次(因此没有必要尝试记忆它)
如果你只需要计算你能以多少种不同的方式获得分数,事情就不同了,因为在这种情况下,子问题的解决方案只是一个数字,你可以用它来解决原始问题。有什么让人困惑的?
void out(int score, int two, int three, int seven, int maxscore)
{
...
else {
if (maxscore >= 7) output(score-7, two, three, seven+1, 7);
if (maxscore >= 3) output(score-3, two, three+1, seven, 3);
if (maxscore >= 2) output(score-2, two+1, three, seven, 2);
}
}