[英]Java: Creating an Ordered Polynomial
给定一个多项式,例如5x^4 - 1x^2 +2x^2 -3 +5x
我创建了以下三个数组,它们的索引与多项式中每个“项”具有的变量,度和系数匹配。
coefficentArray=[5, -3, 2, -1, 5]
variableArray = [x, , x, x, x]
degreeArray = [1, 1, 2, 2, 4] //sorted on
我正在尝试编写将所有具有相同度数和相同变量的“项目”相加的代码,并返回新多项式的字符串,因此,例如,我要在此处查找的是+5x^4 +1x^2 +5x^1 -3x^0
。 到目前为止,这是我的功能...
public static String putItTogether(int[] array,int[] array1, String[] array2, int count)
{
int j = count; //count is number of items in arrays
StringBuilder builder = new StringBuilder();
for (int i=count; i<=0 ; i--) {
int answer = 0;
while (array[j] == array[j-1]) { //array = degrees //array1 = coefficients // array2 = variable
if (array2[j] == array2[j-1]) { //if they both have x in same index
answer = array1[j] + array1[j-1];//add the coefficients
}
j--;
}
String coefficientString = Integer.toString(answer); //create string from answer
String degreeString = Integer.toString(array[j]); //get the degree and turn it into a String
if (answer < 0)
builder.append("-") //if answer negative, add negative sign
else
builder.append("+") // else add positive sign
builder.append(coefficientString); //add coefficient to builder
builder.append("x"); //add "x" to coefficient in builder
builder.append("^"); //add the "^" after the "x"
builder.append(degreeString); //finally add the degree after the "^"
buider.append(" "); //add space inbetween
}
}
return builder.toString();
}
使用顶部给出的多项式作为输入,我收到+0x^0 +0x^0
由于盯着它看了几个小时,似乎找不到任何东西,所以以为我会尝试获取另一个一双眼睛。 感谢您的宝贵时间,我们非常感谢。 另外,如果您认为我应该杀死我的小“代码婴儿”并从另一个角度解决问题,请不要害怕告诉我!
我建议您取消当前的计划。 进行归约逻辑,然后在一个循环中全部输出可能会非常混乱。 相反,您应该使用一种方法进行约简逻辑,然后再使用另一种方法来输出约简逻辑的结果。
现在,对于该方法,度数列表和系数列表非常适合输入,我们将保留它们,但是如果您唯一的变量是x
则不需要变量列表。 我明白了为什么要使用变量数组(用于常量),但要记住常量C = C * x^0
(常量的度数为0,因为x^0=1
),所以基本上方程式中的每个项目都具有变量x
。
现在,对于简化方程式的第一部分,我建议使用一个映射,其中的键是变量的度,而值是系数值。 这将用于跟踪每个度数的系数值。
注意,等式3x^2 + 2x + 3
的映射看起来像
degree --> coefficient
( coefficient x^degree
)
2 --> 3
( 3 x^2
)
1 --> 2
( 2 x^1
)
0 --> 3
( 3 x^0
)
因此reduce算法将如下所示。 。 。
public TreeMap<Integer,Integer> reducePolynomial(int[] coefficients, int[] degrees) {
TreeMap<Integer,Integer> polynomials = new TreeMap<Integer,Integer>();
for(int i = 0; i < coefficients.length; i++) {
int coefficient = coefficients[i];
int degree = degrees[i];
if(polynomials.containsKey(degree)) {
// sum coefficients of the same degree
int currentCoefficient = polynomials.get(degree);
polynomials.put(degree, currentCoefficient + coefficient);
} else {
// no coefficient for the current degree, add it to map
polynomials.put(degree, coefficient);
}
}
return polynomials;
}
程序的输出是度系数图。 然后,您可以使用该映射来构造方程式字符串。 我们将采用每个进入degree --> coefficient
并将其转换为+-coefficient x^degree
public String outputPolynomial(TreeMap<Integer,Integer> polynomials) {
StringBuilder builder = new StringBuilder();
for(Entry<Integer,Integer> polynomial : polynomials.descendingMap().entrySet()) {
if(polynomial.getValue() != 0) {
// append plus (note negative coefficient will print out a minus sign)
if(polynomial.getValue() >= 0) {
builder.append("+ ");
}
// append coefficient
builder.append(polynomial.getValue());
builder.append("x^");
// append degree
builder.append(polynomial.getKey());
builder.append(" ");
}
}
return builder.toString();
}
所有这些背后的想法是将输入列表简化为一个方便的数据结构,然后使用该数据结构输出结果。
最后,如果您想扩展此算法以使用多个变量( y
, z
,...),则可以构造多个映射,每个变量一个,再加上一个变量以跟踪常量值。
编辑:解决评论
+ 0x^0
来自哪里?
那是我写的错误。 我修改了代码,以便如果多项式系数= 0,则不输出当前多项式。
如何按降序输出?
我实现了HashMaps,但没有订单,而TreeMaps有。 您可以创建一个new TreeMap<Integer,Integer>();
那么你可以做类似的事情
NavigableMap <Integer, Integer> polynomialsDesc = treeMap.descendingMap();
for(Entry<Integer,Integer> polynomial : polynomialsDesc) {
... output logic ...
}
我修改了上面的代码以改为使用TreeMap。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.