用Java写 Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three integers x1, x2, x3 and three more integers y1, y2, y3, such that x1 < x2 < x3, y1 < y2 < y3 and the eight point set consists of all points (xi, yj) (1 ≤ i, j ≤ 3), except for point (x2, y2). You have a set of eight points. Find out if Gerald can use this set?
时间: 2023-11-22 15:55:18 浏览: 263
用Java写的一些程序
可以使用以下代码来实现:
```java
import java.util.*;
public class EightPointSet {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] x = new int[4];
int[] y = new int[4];
for (int i = 0; i < 8; i++) {
int xi = sc.nextInt();
int yi = sc.nextInt();
if (i == 0) {
x[1] = xi;
y[1] = yi;
} else if (i == 3) {
x[2] = xi;
y[2] = yi;
} else if (i == 6) {
x[3] = xi;
y[3] = yi;
} else if (xi == x[1] && yi == y[2]) {
x[0] = xi;
y[0] = yi;
}
}
Arrays.sort(x);
Arrays.sort(y);
boolean valid = true;
for (int i = 1; i <= 3; i++) {
if (x[i] - x[i-1] != 1 || y[i] - y[i-1] != 1) {
valid = false;
break;
}
}
if (valid && x[1] != x[0] && x[1] != x[2] && y[1] != y[0] && y[1] != y[2]) {
System.out.println("Gerald can use this set");
} else {
System.out.println("Gerald cannot use this set");
}
}
}
```
首先,我们读入八个点的坐标,并将它们存储在 `x` 和 `y` 数组中。然后,我们找到缺少的那个点(即 `(x2, y2)`),并将其存储在 `x[0]` 和 `y[0]` 中。
接下来,我们对 `x` 和 `y` 数组进行排序,并检查每个相邻的点之间的距离是否为 $1$。如果是,我们还需要检查缺少的那个点是否符合要求。如果所有条件都满足,则可以使用该点集;否则,不能使用该点集。
阅读全文