蛮力递归是一个选项,具体取决于您允许值的大小或数量成为多少。
假设:用户输入(被乘数)始终是不同的正整数。要找到的系数必须是非负整数。
算法:
Of the multiplicands, let M be the largest.
Calculate C=floor(F/M).
If F=M*C, output solution of the form (0,0,...,C) and decrement C
If M is the only multiplicand, terminate processing
Loop from C down to 0 (call that value i)
Let F' = F - i*M
Recursively invoke this algorithm:
The multiplicands will be the current set minus M
The goal value will be F'
For each solution output by the recursive call:
append i to the coefficient list and output the solution