0

次のように述べられている問題を解決しようとしています。セットセグメントとポイントセットが与えられた場合、各ポイントを含むセグメントの数を計算します。

私が遭遇した問題は、ポイントがセグメントに含まれる回数を数えなければならないときです。特定の入力があると、内側のループは各ポイントのカウンターを正しくインクリメントし、別のデータセットがある場合は天気予報を行い、ゼロと負の数と非負の数を比較すると、奇妙な動作をします。

以下は、私が直面している問題を特定するために作成された単なるスクリプトであり、実際の実装を表すものではありません。

テスト ケースでは、次のような出力が得られます。

ケース 1:

    String debug = "Test case 1: \n ";
    debug += " \n - 2 Segments with coordinates [0, 5] and [7, 10].";
    debug += " \n - 3 points at the coordinates 1, 6, and 11.";
    int [] starts = new int[]{0, 7};
    int [] ends = new int[]{5, 10};
    int [] points = new int[]{1, 6, 11};

    debug += "\n \n Calculating the coverage of the points: ";
    for ( int i=0; i<starts.length; i++) {
        for (int j=0; j<points.length && ( starts[i] <= points[j] && points[j] <= ends[i]); j++) {
            debug += " \n * Point with coordinate " + points[j] + ", is between " + starts[i] + " and " + ends[i];
        }
    }
    debug += "\n \n FINISHED the calculation!";

    int start = 0, point = 1, end = 5;
    debug += "\n \n Custom check for the 1st point: ";
    debug += "\n - Is (" + start + " <= " + point + " and " + point + " <= " + end + ")? " + ( start <= point && point <= end );
    System.out.println(debug);

出力:

テスト ケース 1:

  • 2 座標 [0, 5] および [7, 10] のセグメント。
  • 座標 1、6、および 11 の 3 つのポイント。

    ポイントのカバレッジの計算:

  • 座標 1 の点は、0 から 5 の間です

    計算終了!

    1 点目のカスタム チェック:

  • (0 <= 1 かつ 1 <= 5) ですか? 真実

ケース 2:

    String debug = "Test case 2: \n ";
    debug += " \n - 1 Segment with coordinates [-10, 10].";
    debug += " \n - 3 points at the coordinates -100, 100, and 10.";
    int [] starts = new int[]{-10};
    int [] ends = new int[]{10};
    int [] points = new int[]{-100, 100, 0};

    debug += "\n \n Calculating the coverage of the points: ";
    for ( int i=0; i<starts.length; i++) {
        for (int j=0; j<points.length && ( starts[i] <= points[j] && points[j] <= ends[i]); j++) {
            debug += " \n * Point with coordinate " + points[j] + ", is between " + starts[i] + " and " + ends[i];
        }
    }
    debug += "\n \n FINISHED the calculation!";

    int start = -10, point = 0, end = 10;
    debug += "\n \n Custom check: ";
    debug += "\n - Is (" + start + " <= " + point + " and " + point + " <= " + end + ")? " + ( start <= point && point <= end );
    System.out.println(debug);

出力:

テスト ケース 2:

  • 1 座標 [-10, 10] のセグメント。
  • 座標 -100、100、および 10 の 3 点。

    ポイントのカバレッジの計算:

    計算終了!

    カスタム チェック:

  • (-10 <= 0 かつ 0 <= 10) ですか? 真実

ご覧のとおり、内側のループの条件は、セグメント [-10, 10] に対して、座標 0 の点の場合を適切に計算していません。

前もってありがとう、エンドリット。

4

2 に答える 2