首先让我澄清一下(在你们解雇我之前),这不是作业问题,我也不是大学生。 :)
编辑 感谢@Klas 和其他人,我的问题现在归结为需要以编程方式求解的数学方程式。
我正在寻找求解Linear Diophantine Equation
的算法/代码。
对于像我这样的凡人来说,下面是这样一个等式:
示例 1:3x + 4y + 5z = 25
(找到 x、y、z 的所有可能值)
示例 2:10p + 5q + 6r + 11s = 224
(找到 p、q、r、s 的所有可能值)
示例 3:8p + 9q + 10r + 11s + 12t = 1012
(找到 p、q、r、s、t 的所有可能值)
我尝试谷歌搜索无济于事。我本以为已经编写了一些代码来解决这个问题。如果你们遇到过某种已经实现了这个的库,请告诉我。如果解决方案是用 Java 编写的,那就再酷不过了!算法/伪代码也可以。非常感谢。
最佳答案
暴力递归是一种选择,具体取决于您允许值的大小或值的数量。
假设:用户输入(被乘数)总是不同的正整数。求出的系数必须是非负整数。
算法:
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
关于java - 求解线性丢番图方程(参见示例说明),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5513129/