java - 二分查找期间堆栈溢出

标签 java stack-overflow bisection

我正在使用二分搜索来找到行星之间的平衡点。 BinaryBalance 方法接受行星数组列表,它是一个具有位移和质量属性的对象。它还考虑了两个行星的位移,我试图在这两个行星之间找到平衡点。 Double x 是搜索的初始起点,我在这里设置 p1 和 p2 的平均位移。该代码运行顺利,但它与答案有一点偏差。我尝试通过将误差间隔设置为大于 1e-10 来提高精度,但我不断收到 Stack Overflow 错误。如何以更高的精度解决这个问题?

import java.util.*;
import java.lang.*;

public class Solution {
public static void main(String[] arg) {
    Scanner sc = new Scanner(System.in);
    int numCase = sc.nextInt();

    for (int k = 1; k <= numCase; k++) {

        //Initializing Space...
        int numPlanets = sc.nextInt();
        ArrayList<Planet> space = new ArrayList<>();
        int[] weights = new int[numPlanets];
        int[] displacements = new int[numPlanets];

        for (int i = 0; i < numPlanets; i++) {
            displacements[i] = sc.nextInt();
        }
        for (int i = 0; i < numPlanets;i++) {
            weights[i] = sc.nextInt();
        }
        for (int i = 0; i < numPlanets;i++) {
            Planet p = new Planet(displacements[i],weights[i]);
            space.add(p);
        }

        System.out.print("#" + k + " ");
        for (int i = 0; i < numPlanets-1; i++) {
            double init = (double) (space.get(i).getDisplacement() + space.get(i+1).getDisplacement()) /2;
            binaryBalance(space,space.get(i).getDisplacement(),space.get(i+1).getDisplacement(),init);
        }
        System.out.println();
    }
}

public static class Planet {
    private int d;
    private int m;

    public Planet(int d,int m) {
        this.d = d;
        this.m = m;
    }

    public void setDisplacement(int d) {
        this.d = d;
    }

    public void setMass(int m) {
        this.m = m;
    }

    public double getG(double dPlanet) {
        double betweenDistance = this.d - dPlanet;
        return this.m/(betweenDistance*betweenDistance);
    }

    public int getDisplacement() {
        return d;
    }
    public int getMass() {
        return m;
    }
}

public static void binaryBalance(ArrayList<Planet> space, double p1, double p2, double x) {
    double leftg = 0;
    double rightg = 0;
    for (int i = 0; i < space.size(); i++) {
        if (space.get(i).getDisplacement() < x) {
            leftg = leftg + space.get(i).getG(x);
        } else {
            rightg = rightg + space.get(i).getG(x);
        }
    }

    if (Math.abs(leftg - rightg) < 1e-10) {
        System.out.print(String.format("%.10f",x) + " ");
        return;
    }
    if (leftg < rightg) {
        binaryBalance(space, p1, x, (p1 + x) / 2);
    } else {
        binaryBalance(space, x, p2, (p2 + x) / 2);
    }
}

测试用例是:

10
2
1 2 1 1
2
1 2 1 1000
2
457 468 333 321
3
1 2 3 1 2 1
4
2 3 5 7 3 2 7 5
5
3 11 12 19 29 542 661 450 521 366   
6
42 75 88 94 113 144 669 551 355 344 294 155
7
62 86 279 323 363 516 579 810 749 736 297 136 107 52
8
10 34 64 73 93 97 101 122 466 463 441 373 315 292 225 83
10
9 14 38 39 48 73 179 190 207 302 560 497 640 722 437 259 449 470 709 520

预期的答案是:

#1 1.5000000000
#2 1.0306534300
#3 462.5504629633
#4 1.4060952085 2.5939047915
#5 2.5328594461 3.7271944335 6.0999536409
#6 6.3428568767 11.5477377494 15.9641592998 24.9267991615
#7 57.8805685415 81.8651598883 91.0573691382 105.0835650491 133.2934094881
#8 74.2211477711 190.6837563313 305.8269181686 348.3304429927 470.2694219293 555.4943093854
#9 21.5171374463 47.9890597763 68.6536668433 82.9131954023 95.0052272762 99.1999097770 116.4978330953
#10 11.5573600056 24.0238341337 38.4847676134 44.6137453708 64.7500445424 126.9908128982 184.3221650927 197.9760596291 266.0574653677

最佳答案

在质量差异如此之大的第二种情况下,leftg-rightg 容差为 1e-10,最大迭代次数为 47 次。这不会溢出任何堆栈,但当然您询问了提高准确性的问题。不幸的是,由于涉及的数字规模很大(正如我在评论中提到的),案例 6 甚至不可能达到 1e-11。因此,如果您完全更改容差指数,就会得到无限递归。

但也许固定平衡容差并不是您在本练习中需要做的!如果我改为细化直到间隔的宽度为“零”,我就会得到准确的预期答案(达到给定的精度)。 (p1p2 不必相等,但它们之间没有 float 。您可以通过注意到 x==p1 || x==p2 来检测这一点;x 将以二进制 0 结尾。)对于这些情况,最多需要 53 个二等分:任何数值分析人员都应该熟悉这个数字,因为它是有效位数和一个double。我没有检查较大的 p2-p1 容差是否可以给出正确的答案。

由于深度超过 53 层是没有用的,所以这里选择递归是无害的(尽管带有 void 函数的尾递归看起来很奇怪),并且更改为迭代根本没有帮助。不管怎样,你必须确保它终止!

关于java - 二分查找期间堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179008/

相关文章:

python - 为什么即使值不存在,python 的 bisect_left 也会返回有效索引?

java - 了解何时访问数据库与缓存

flash - 为什么在 Actionscript 3 中使用超过 2 个参数调用此函数会导致堆栈溢出?

c# - Unity Networking : Unity crashes when I call the NetworkManager. singleton.StopClient() 函数

c++ - SIGSEGV 在 C++ 循环中有一个非常大的数组

C++ 错误 : no matching function for call

java - 在 Android 开发中如何绘制矩阵?

java - 是否可以设置一个计时器来显示/隐藏 Material 设计中的密码?

java - 非静态内部类中静态最终对象的编译错误

c++ - 使用 boost::bind 将成员函数绑定(bind)到 boost::bisect?