這是我使用喜歡列表乘以兩個多項式的代碼。它工作正常,但問題是如果我乘以(3x^2 + 5x + 3)*(4x^3 + 5^x +2)在java中使用鏈接列表乘以多項式
我得到結果爲12x^5 + 15x^2 + 6x^2 + 20x^4 + 25x^2 + 10x + 12x^3 + 15x +6。
但我怎麼可以把它與同類指數是togther增加等,使得它輸出方面12X^5 + 43X^2 + ..
public class LinkedPoly{
static String exponent="";
Node2 head;
Node2 current;
LinkedPoly(){
head=null;
}
public void createList(int c,int e){
head=new Node2(c,e,head);
}
public static LinkedPoly multiply(LinkedPoly list1,LinkedPoly list2){
Node2 temp1=list1.head;
Node2 temp2=list2.head;
Node2 temp3=temp2;
LinkedPoly multiplyList=new LinkedPoly();
while(temp1!=null){
while(temp2!=null){
multiplyList.createList((temp1.coef*temp2.coef),(temp1.exp+temp2.exp));
temp2=temp2.next;
}
temp2=temp3;
temp1=temp1.next;
}
return multiplyList;
}
相關:http://stackoverflow.com/questions/24394860 – dedek