我目前正在编写一个方法,将两个多项式(由2个文本文件给出)加在一起。例如:
4.0x ^ 5 + -2.0x ^ 3 + 2.0x + 3.0
&安培;
8.0x ^ 4 + 4.0x ^ 3 + -3.0x + 9.0
会导致:4.0x ^ 5 + 8.0x ^ 4 + 2.0x ^ 3 - 1.0x + 12
目前,我的方法创建了一个新的多项式对象,但只添加了一个度数匹配的术语。所以我的输出看起来像这样:
2.0x ^ 3 - 1.0x + 12
缺少前两个术语,因为学位不匹配。这是我的代码(重要的是:多项式最初由Node poly = null构成; - 所以变量poly是指向多项式的Linked List前面的节点):
public Polynomial add(Polynomial p) {
Polynomial answer = new Polynomial();
for (Node firstPoly = poly; firstPoly != null; firstPoly = firstPoly.next){
for (Node secondPoly = p.poly; secondPoly != null; secondPoly = secondPoly.next){
if (firstPoly.term.degree == secondPoly.term.degree){
answer = addToRear(answer, (firstPoly.term.coeff + secondPoly.term.coeff), firstPoly.term.degree, null);
if (answer.poly.term.coeff == 0){
answer.poly = null;
}
}
}
}
return answer;
}
我不是要求任何人为我解决这个问题,但有没有人知道我接下来要做什么以确保添加不匹配的学位?我一直试图在纸上解决这个问题,但由于各种原因,没有任何问题。这是addToRear方法,以防万一它对你们有用。
private Polynomial addToRear(Polynomial p, float coeff, int degree, Node next){
if (p.poly == null){
p.poly = new Node(coeff, degree, null);
return p;
}
for (Node temp = p.poly; temp != null; temp = temp.next){
if (temp.next == null){
temp.next = new Node(coeff, degree, null);
return p;
}
}
return p;
}
谢谢。
答案 0 :(得分:1)
可能更容易确保多项式数据结构包括所有功率,包括具有零乘数的功率。换句话说,多项式2.0x3 - 1.0x + 12
将表示为集合:
pwr 0 1 2 3
---- ---- ---- ----
{ 12.0, 1.0, 0.0, 2.0 }
除非您正在谈论大个多项式数和大量幂对于高端术语,否则此解决方案的非稀疏性应该大部分无关紧要
正如您所看到的,我还更改了顺序,以便x0
(常量)字词在列表中排在第一位,假设您的多项式不是“有负面的力量,这也将减轻增加的努力。这是因为匹配的权力将在集合中具有匹配的索引。
所以,要将两个多项式加在一起:
4.0x5 - 2.0x3 + 2.0x + 3.0
{
{1}}
类似于:
8.0x4 + 4.0x3 - 3.0x + 9.0
根据需要给出(在输出时忽略零乘数)
pwr 0 1 2 3 4 5 ---- ---- ---- ---- ---- ---- { 3.0, 2.0, 0.0, -2.0, 0.0, 4.0 } + { 9.0, -3.0, 0.0, 4.0, 8.0 } ---------------------------------------- = { 12.0, -1.0, 0.0, 2.0, 8.0, 4.0 }
如果出于某种原因,您必须在稀疏链接列表上工作,那么这取决于这些术语是否根据权力进行排序。
如果他们不这样做,您通常必须使用如下算法:
4.0x5 + 8.0x4 + 2.0x3 - 1.0x + 12.0
这将首先在第一个多项式中添加所有幂,包括在第二个多项式中有条目的那些。
然后它将在第二个中添加那些在第一个中没有相应术语的那些。这样,所有条款都正确添加。
如果术语 已排序,您可以通过并行处理列表(类似于合并算法)使其更高效,在每个阶段从最高的列表中获取术语未经处理的权力,如:
set poly3 to empty
# Process all in poly1, including those in poly2.
foreach term1 in poly1:
find term2 in poly2 with matching power
if none:
add (term1.coeff, term1.power) to poly3
else:
add (term1.coeff + term2.coeff, term1.power) to poly3
# Process all in poly2, but NOT in poly1.
foreach term2 in poly2:
find term1 in poly1 with matching power
if none:
add (term2.coeff, term2.power) to poly3
作为概念证明,这里有一些Python代码可以执行已排序的变体。大部分代码都将字符串转换为"列表" (实际上是一个功率稀疏数组)并打印出所得的多项式。解决方案的主要内容是从set poly3 to empty
set term1 to poly1.head
set term2 to poly2.head
# Process both until at least one runs out.
while term1 != null and term2 != null:
if term1.power == term2.power:
add (term1.coeff + term2.coeff, term1.power) to poly3
term1 = term1.next
term2 = term2.next
elif term1.power > term2.power:
add (term1.coeff, term1.power) to poly3
term1 = term1.next
else:
add (term2.coeff, term2.power) to poly3
term2 = term2.next
# Process remaining single list, if any.
while term1 != null:
add (term1.coeff, term1.power) to poly3
term1 = term1.next
while term2 != null:
add (term2.coeff, term2.power) to poly3
term2 = term2.next
开始:
poly3 = []
答案 1 :(得分:0)
取多项式的最大度数,并将所有缺失项设置为零。即,x ^ 2 = 0x ^ 3 + 1x ^ 2 + 0x ^ 1 + 0x ^ 0。 (作为旁注,数字计算的实际实现将以嵌套形式表示多项式,以最小化乘法后添加的精度损失。)