返回递归中的数字集
以下代码搜索多项式函数的一个零点。它使用递归技术:
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;
}
我想知道如何返回所有零点。我的想法是使用数组,但我不知道该怎么做。有什么想法吗?
我不允许使用数组以外的任何东西(例如不允许使用哈希表或集合)
following code searches for one zero point of a polynomial function. It uses recursion technique:
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;
}
I am wondering how to return all zero points. My ideas is to use an array but I am not sure how to do that. Any ideas?
I am not allowed to use anything else than arrays (e.g. hash tables or sets are not allowed)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会将一个集合(例如 HashSet)传递给您的函数,并将您发现的所有数字放入其中。
正如您所说,您只能使用数组,那么您大概知道可以找到的零点的最大数量,因此创建一个该大小的数组,为每个元素分配 NaN ,然后传入该数组和最大当前索引它的每个函数调用。您需要返回数组的新大小作为结果,以便您始终知道找到了多少个数字。
I would pass in a Collection such as a HashSet to your functions and put all the numbers you discover into it.
As you say you can only use arrays, then you know the maximum number of zero-points that can be found, presumably, so create an array of that size, assigned NaN to every element, then pass in that array and the maximum current index of it to every function call. You will need to return the new size of the array as the result so that you always know how many numbers have been found.
这是一些解释,因为这是家庭作业:
1)我们需要创建一个您将用于存储零点的数据类型的数组。我的猜测是双重的。
2) 数组需要某种类型的起始大小,为了便于讨论,可以说
10
3) 在
nullstelle
方法中,我们将向在第 1 步
中创建的数组添加一个元素4) 如果数组的大小为
LIMIT -1
,我们将数组复制到大小等于LIMIT * 2
的新数组5) 我们现在返回这个数组。
如果需要排序,我们可以使用要决定的任何排序技术遍历列表。
Here is some explanation as this is homework:
1) We need to create an array of the data type you will be using to store zero points. My guess is double.
2) The array will need some type of starting size, lets say
10
for the sake of discussion3) In the
nullstelle
method we will add an element to the array created instep 1
4) If the array has size
LIMIT -1
we copy the array to a new array with size equal toLIMIT * 2
5) We now return this array.
If it needs to be sorted we can walk over the list utilizing whatever sort technique is to be decided on.
首先,您的解决方案目前面临与 您之前的解决方案相同的问题之一问题,因为您不能保证零计算完全正确。不幸的是,这次您不能使用检查 a 和 b 是否具有相反符号的简单解决方案,因为对于任意多项式,零不一定意味着函数穿过 y = 0 线,例如 y = x^4。
不管怎样,要回答你的问题:
Double
类型的大小为 4 的数组。使用 null 表示数组槽为空。由于您的函数是四次函数,因此最多有四个零。没有提供实际的 Java,因为这是家庭作业。
First of all your solution as it stands suffers from one of the same problems as your previous question in that you can't guarantee that you'll get the zero calculation exactly correct. Unfortunately, this time you can't use the simple solution of checking that a and b have opposite signs because, for an arbitrary polynomial, a zero need not imply the function crosses the y = 0 line e.g. y = x^4.
Anyway, to answer your question:
Double
. Use null to denote that an array slot is empty. As your function is a quartic, there is a maximum of four zeroes.No actual Java provided because this is homework.
Math.pow 是一个非常昂贵的函数。您可以使用嵌套表达式完全避免它。 (而且它更短)
Math.pow is a very expensive function. You can avoid it entirely using nested expressions. (And its shorter)