给X轴上N个点,这N个点可以组成 N*(N – 1)/ 2 条线,求第K条线段的长度
如Points : [0, 1, 3, 5] ,当K = 5时,结果是4
package other;
public class Main {
public static void main(String args[]) {
Main main = new Main();
int n = 4;
int points[] = { 0, 1, 3, 5 };
int k = 5;
int resurt = main.getLineLength(points, n, k);
System.out.println(resurt);
}
public int getLineLength(int[] points, int n, int k) {
int length = n * (n - 1) / 2;
int lineLengths[] = new int[length];
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
lineLengths[--length] = points[j] - points[i];
}
}
return lineLengths[lineLengths.length-k];
}
}