广联达7月22日笔试题解
第一题:给定一个数组,取四条边组成最大平行四边形的面积,输入数组,输出最大面积。
把数组排序并取个数超过2的最大的两个即可
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long[] lines = new long[n];
int i = 0;
while (i < n) {
lines[i++] = scanner.nextInt();
}
Arrays.sort(lines);
List<Long> side = new ArrayList<>();
int count = 1;
for (i = n - 1; i >= 0; i--) {
if (side.size() >= 2) {
break;
}
if (i > 0 && lines[i] == lines[i - 1]) {
count++;
} else {
if (count >= 4) {
side.add(lines[i]);
side.add(lines[i]);
} else if (count >= 2) {
side.add(lines[i]);
}
count = 1;
}
}
if (side.size() < 2) {
System.out.println(-1);
} else {
System.out.println(side.get(0) * side.get(1));
}
}
} 第二题:有一个操可以把数组的一个元素提到0号位置,给定一个数组,输出至少多少次这样的操作使数组变得有序。
先把数组排序,如array [2 1 3 4]变为sortArray[1 2 3 4],从后向前遍历两个数组,当array中的数值不等于sortArray中的值时,跳过(这个值也就是要变换位置的值)。AC100%
import java.util.Arrays;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] array = new int[n];
int[] sortArray = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scanner.nextInt();
sortArray[i] = array[i];
}
Arrays.sort(sortArray);
int c = 0;
int j = n - 1;
for (int i = n - 1; i >= 0; i--) {
if(array[i] == sortArray[j]){
++c;
--j;
}
}
System.out.println(n-c);
}
}
第三题:怪兽在一个一维的数轴上,他们有属性位置和血量,玩家可以定点攻击,攻击范围为[x-range, x+rangel,求最少的攻击次数能消灭所有的怪物。 把怪兽按位置由小到大排序,一直攻击,直到把最左边的打死,顺便打死点p + 2 * range之间的,最左边的死了,攻击它右边第一个血量>0的,直到都打死。AC100%
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
int time = 0;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int range = sc.nextInt();
int[][] position = new int[n][2];
for (int i = 0; i < n; i++) {
position[i][0] = sc.nextInt();
position[i][1] = sc.nextInt();
}
Arrays.sort(position, Comparator.comparingInt(p -> p[0]));
for (int i = 0; i < n; i++) {
while (position[i][1] > 0) {
int tmp = position[i][1];
for (int j = i; j < n; j++) {
if (position[j][0] - position[i][0] <= range * 2) {
position[j][1] -= tmp;
}
}
time += tmp;
}
}
System.out.println(time);
}
} 