Java:创建一个有序多项式
例如,给定一个多项式,Java:创建一个有序多项式,java,if-statement,for-loop,stringbuilder,polynomial-math,Java,If Statement,For Loop,Stringbuilder,Polynomial Math,例如,给定一个多项式,5x^4-1x^2+2x^2-3+5xI创建以下三个数组,其索引与多项式中每个“项”具有的变量、阶数和系数匹配 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
5x^4-1x^2+2x^2-3+5x
I创建以下三个数组,其索引与多项式中每个“项”具有的变量、阶数和系数匹配
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();
}
公共静态字符串putitother(int[]array,int[]array1,String[]array2,int count)
{
int j=count;//count是数组中的项数
StringBuilder=新的StringBuilder();
对于(int i=count;i我建议终止您当前的计划。执行reduce逻辑然后在一个循环中输出所有内容可能会非常混乱。相反,您应该在一个方法中执行reduce逻辑,然后在另一个方法中打印reduce逻辑的结果
对于这种方法,度数列表和系数列表对于输入非常有用,我们将保留它们,但如果您的唯一变量是x
,则不需要变量列表。我明白为什么您有变量数组(用于常量),但请记住常量C=C*x^0
(一个常数的度数为0,即x^0=1
),因此基本上方程中的每一项都有一个变量x
现在,对于简化方程的第一部分,我建议使用一个映射,其中关键是变量的阶数,值是系数值。这将用于跟踪每个阶数的系数值
注意,方程3x^2+2x+3
的映射如下
度-->系数
(系数x^度
)
2-->3
(3x^2
)
1-->2
(2x^1
)
0-->3
(3x^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;
}
所有这些背后的想法是将输入列表简化为一个方便的数据结构,然后使用该数据结构输出结果
最后,如果您想扩展此算法以使用多个变量(y
,z
,…),那么您可以构建多个映射,每个变量一个,加上一个变量以跟踪常量值
编辑:处理评论
+0x^0
从哪里来
这是我写的一个错误。我修改了代码,如果多项式系数=0,那么就不输出当前多项式
如何按降序输出
我实现了HashMaps,它没有顺序,但树映射有顺序
NavigableMap <Integer, Integer> polynomialsDesc = treeMap.descendingMap();
for(Entry<Integer,Integer> polynomial : polynomialsDesc) {
... output logic ...
}
NavigableMap polynomialdesc=treeMap.downingmap();
用于(输入多项式:PolynomialDescrc){
…输出逻辑。。。
}
我修改了上面的代码,改为使用树形图。请修改您的问题,发布的代码中有几个语法错误,当这些错误被纠正时,输出是一个空字符串,因为它甚至没有为
循环输入主。非常好,我考虑过使用地图和键,但不确定如何实现我用java编写代码已经有一段时间了。我问了两个问题,我输入了5x^2+6x^2-5x+2x^2+5x^3
,它返回了+0x^0-5x^1+13x^2+5x^3
。我该如何使它+5x^3+13x^2-5x^1
,基本上改变了顺序,而
从何而来?改变顺序最高的学位排在第一位,然后是第二位,第二位,依此类推(int i=coefficents.length;除了一件事之外,树形图代码起到了作用。假设我有5x^2+5+5x^2
,结果只有10x^2
,并且没有注册5,我该如何将5添加进去呢?如果是5x^0,这就是我在本例中寻找的,我用输入系数测试了这些方法={5,5,5}和degrees={2,0,2}
,结果是+10x^2+5x^0
,因此看起来是正确的。请确保将方程正确解析为系数/度数组。
NavigableMap <Integer, Integer> polynomialsDesc = treeMap.descendingMap();
for(Entry<Integer,Integer> polynomial : polynomialsDesc) {
... output logic ...
}