我正在尝试解决如下所述的问题:给定一组段和一组点,计算每个点包含多少段。
我遇到的问题是当我必须计算一个点被段包含多少次时。当我有某个输入时,内部循环会正确递增每个点的计数器,当我有另一个数据集时,天气会比较零与负数和非负数,它的行为很奇怪。
以下只是为了定位我面临的问题而创建的脚本,并不代表实际的实现。
测试用例给出如下输出:
情况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)吗?真的
案例二:
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)吗?真的
如您所见,内部循环的条件在某种程度上没有正确计算坐标为 0 的点相对于段 [-10, 10] 的情况。
在此先感谢,恩德里特。