我正在尝试添加两个具有不同系数数和不同次数的多项式。我需要按度数升序打印最终结果。
我尝试使用两个for循环,并比较两个多项式中的次数,如果次数相同,则添加系数。这在具有相同阶数的系数的方程中起作用
public Polynomial add(Polynomial second)//add function
{
for(int i=0;i<degree.length;i++)
{
for(int j=0;j<degree.length;j++)
{
if(this.degree[i]==second.degree[j])
{
this.coeff[i]=this.coeff[i]+second.coeff[j];
}
}
}
Polynomial result=new Polynomial();
result=this;
return result;
}输入和结果:
poly 1:-4x1+6x4+7x3
poly 2:-6x1+2x2
expected is 10x1+2x2+7x3+6x4
actual is 10x1+6x4+7x3发布于 2019-11-13 02:16:19
你在逻辑中遗漏了一个条件。您需要添加不匹配的变量(存在于一个多项式中)。但是你把它与第二个多项式的匹配加到了第一个多项式上。
https://stackoverflow.com/questions/58823745
复制相似问题