poj2785java
时间: 2023-12-31 22:24:11 浏览: 108
POJ.rar_poj java_poj1048
以下是使用Java解决POJ 2785问题的示例代码:
```java
import java.util.Arrays;
public class POJ2785 {
public static void main(String[] args) {
int[] A = {1, 2, -3, 4, -1, -2, 3, -4};
int n = A.length;
int[] x = new int[n * n];
int[] y = new int[n * n];
int idx = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[idx] = A[i] + A[j];
y[idx] = -(A[i] + A[j + 2]);
idx++;
}
}
Arrays.sort(y);
int count = 0;
for (int i = 0; i < n * n; i++) {
int target = x[i];
int left = 0;
int right = n * n - 1;
while (left < right) {
int mid = (left + right) / 2;
if (y[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
if (y[left] == target) {
count++;
}
}
System.out.println(count);
}
}
```
阅读全文