2011-03-15 54 views
0

我需要做一个递归方法多项式添加(多项式p),使用递归将其添加到p。我读过java有add(Polynomial p)方法,但这不是递归的。使用递归添加多项式

我最好的尝试,到目前为止一直是这个:

public class Polynomial { 
int[] coef; 
int degree; 

public int deeg() { 
int d = 0; 
for (int r = 0; r < coef.length; r++) 
if (coef[i] != 0) d = r; 
return d; 
} 

public Polynomial addition(Polynomial p) { 
Polynomial apple = this; 
Polynomial orange = new Polynomial(0, Math.max(apple.degree, orange.degree)); 
for (int r = 0; r <= apple.degree; i++) orange.coef[r] += apple.coef[r]; 
for (int r = 0; r <= p.degree; r++) orange.coef[r] += p.coef[i]; 
orange.degree = orange.deeg(); 
return orange; 
} 
} 

但同样,这不是递归。

回答

0

为了得到递归,你需要调用此方法中的一个方法,这个类中没有创建一个类:

public void add(List numbers) { 
    // do stuff 
    if (condition) 
     add(numbers); 
} 
0

我在想,他们可能要你做,你添加的东西两个多项式的x^0项,并将其附加到x *(sum(poly1/x,poly2/x))...我知道/ x不起作用,但通常这是递归加法完成。

- 除了lisp之外,我几乎没有看到任何东西做递归添加。