以下代码搜索多项式函数的一个零点。它使用递归技术:在递归中返回一组数字
private static double funktion(int[] koef, double x){
return koef[0] * Math.pow(x,4) + koef[1] * Math.pow(x,3) +
koef[2] * Math.pow(x,2) + koef[3]*x + koef[4];
}
private static double nullstelle(double a, double b, int[] koef){
double middle = (a + b)/2;
double result = middle;
if(Math.abs(a-b) > 0.00001){
double sin = funktion(koef, middle);
if(sin == 0){
result = middle;
}else if(Math.signum(funktion(koef, a)) ==
Math.signum(funktion(koef, middle))){
result = nullstelle(middle, b, koef);
}else{
result = nullstelle(a, middle, koef);
}
}
return result;
}
我想知道如何返回所有的零点。我的想法是使用一个数组,但我不知道如何做到这一点。有任何想法吗?
我不允许使用任何比阵列其他(如哈希表或设置是不允许的)
数组是否需要排序? – Woot4Moo 2010-11-04 14:33:24
否可能未排序 – 2010-11-04 14:40:59
好的。我提供了以下对您有价值的步骤。 – Woot4Moo 2010-11-04 14:41:33