gpt4 book ai didi

java - 基于线性方程计算值的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:10 24 4
gpt4 key购买 nike

我正在使用 java 计算基于线性方程的 PayStructure 中各种 Paycode 的值。我的不同方程式如下:

CTC = Fixed Value
Basic = CTC * 0.4
HRA = Basic/2
ConveyanceAllowance = Fixed Value
ProvidentFund = Basic * 0.12
Gratuity = Basic * .0481
OtherAllowance = (CTC - (Basic + HRA + ConveyanceAllowance + ProvidentFund + Gratuity))

我已经尝试使用给出的解决方案 here .但是这个解决方案只适用于所有计算值都是整数的情况,在我的例子中,这些值也可以包含小数。我根据上述条件修改后的代码如下:

public class PayStructure {

public static void main(String[] args) {
findAndprintSolutions(1, 1000000);
}

private static void findAndprintSolutions(int from, int to) {
for (int a = from; a < to; a++) {
for (int b = from; b < to; b++) {
for (int c = from; c < to; c++) {
for (int d = from; d < to; d++) {
for (int e = from; e < to; e++) {
for (int f = from; f < to; f++) {
for (int g = from; g < to; g++) {
if (isSolution(a, b, c, d, e, f, g))
printSolution(new int[] { a, b, c, d, e, f, g });
}
}
}
}
}
}
}
}

private static boolean isSolution(int a, int b, int c, int d, int e, int f, int g) {
if (a != 100000)
return false;
if (b != a * (.4))
return false;
if (c != b / 2)
return false;
if (d != 10000)
return false;
if (e != b * (.12))
return false;
if (f != b * (.0481))
return false;
if (g != (a - (b + c + d + e + f)))
return false;
return true;
}

private static void printSolution(int[] variables) {
StringBuilder output = new StringBuilder();
for (int variable : variables) {
output.append(variable + ", ");
}
output.deleteCharAt(output.length() - 1);
output.deleteCharAt(output.length() - 1);
System.out.println(output.toString());
}

}

此外,上述代码将终止,因为 CTC 的最大值可能为数百万,并且根据变量的数量,时间复杂度最终将达到 millions^NumberOfVariables。是否有任何其他可能性可以根据给定的方程式计算这些值?方程和变量的数量可能会有所不同,但会有一个解决方案来计算每个变量的值,因此通用解决方案的任何输入都会更好。

E.g.: If CTC = 100000 and ConveyanceAllowance = 10000, the code should return the output as:
Basic = 40000
HRA = 20000
ProvidentFund = 4800
Gratuity = 1924
OtherAllowance = 23276

最佳答案

也许您最好的选择是弄清楚如何将其转化为 c[1]x[1] + c[2]x[2] 形式的线性方程组的形式 ... + c[n]x[n] = 0。从那里,您可以使用广泛建立的线性系统技术来求解系统。查看Wikipedia大量信息的页面。您可以让用户以这种形式向您的方法提供输入,或者您可以对每个方程进行少量处理以对其进行转换(例如,如果所有方程都像您的示例一样在 LHS 上有一个变量,则翻转签名并将其放在 RHS 的末尾)。

解释求解线性方程组的理论超出了这个答案的范围,但基本上,如果存在一个有效分配,您的系统将被唯一确定,如果不存在有效分配则为超定,或者如果存在无限多个则为欠定分配是可能的。如果有一个独特的任务,你会得到号码;如果系统不确定,你至少会得到一组约束,这些约束必须包含无限多解中的任何一个;如果不确定,您将一无所获并且知道原因。

关于java - 基于线性方程计算值的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55926129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com