先是14道填空题
然后4道编程
第一题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] t = s.split(",");
int[] arr = new int[t.length];
for (int i = 0; i < t.length; i++) {
arr[i] = Integer.parseInt(t[i]);
}
in.close();
int sum = sumOfOddLengthSubarrays(arr);
System.out.println(sum);
}
public static int sumOfOddLengthSubarrays(int[] arr) {
int n = arr.length;
int totalSum = 0;
int[] prefixSum = new int[n + 1];
// 构建前缀和数组
for (int i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
}
// 计算所有奇数长度子数组的和
for (int len = 1; len <= n; len += 2) { // 只计算奇数长度
for (int start = 0; start <= n - len; start++) {
int end = start + len - 1;
totalSum += prefixSum[end + 1] - prefixSum[start];
}
}
return totalSum;
}
}
第二题
import java.util.*;
class Tuple {
public int level;
public int price;
Tuple(int level, int price) {
this.level = level;
this.price = price;
}
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static long maxSum = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int N = in.nextInt();
int x = in.nextInt();
List<Tuple> tuples = new ArrayList<>();
for (int i = 0; i < N; i++) {
int level = in.nextInt();
int price = in.nextInt();
tuples.add(new Tuple(level, price));
}
findMaxPriceSum(tuples, -1, -1, 0, 0, x);
System.out.println(maxSum);
}
public static void findMaxPriceSum(List<Tuple> tuples, int minLevel,
int maxLevel, int index, long currentSum, int x) {
if (index >= tuples.size()) {
maxSum = Math.max(maxSum, currentSum);
return;
}
findMaxPriceSum(tuples, minLevel, maxLevel, index + 1, currentSum, x);
if (minLevel == -1 && maxLevel == -1) {
findMaxPriceSum(tuples, tuples.get(index).level, tuples.get(index).level,
index + 1, tuples.get(index).price + currentSum, x);
}
int minn = Math.min(minLevel, tuples.get(index).level);
int maxx = Math.max(maxLevel, tuples.get(index).level);
if (maxx - minn <= x) {
findMaxPriceSum(tuples, minn, maxx, index + 1,
tuples.get(index).price + currentSum, x);
}
}
}
第三题
输入
7 7
192.168.0.1 1
192.168.0.2 2
192.168.0.3 3
192.168.0.4 4
192.168.0.5 5
192.168.0.6 6
192.168.0.7 7
1 2
2 3
1 3
3 6
6 7
2 7
4 5
5
192.168.0.1 192.168.0.1
192.168.0.1 192.168.0.2
192.168.0.1 192.168.0.6
192.168.0.1 192.168.0.7
192.168.0.3 192.168.0.4
输出
0
1
2
2
-1
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static int n;
private static int m;
private static int q;
private static int[][] graph;
private static Map<String, Integer> idMap;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
in.nextLine();
idMap = new HashMap<>();
graph = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
String s = in.nextLine();
String[] t = s.split(" ");
int tt = Integer.parseInt(t[1]);
// System.out.println(tt);
idMap.put(t[0], Integer.parseInt(t[1]));
Arrays.fill(graph[i], Integer.MAX_VALUE);
graph[i][i] = 0;
}
Arrays.fill(graph[0], Integer.MAX_VALUE);
for (int i = 0; i < m; i++) {
int from = in.nextInt();
int to = in.nextInt();
graph[from][to] = 1;
graph[to][from] = 1;
}
for (int k = 0; k < n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (graph[i][k] != Integer.MAX_VALUE && graph[k][j] != Integer.MAX_VALUE &&
graph [i][k] + graph[k][j] < graph[i][j]) {
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}
q = in.nextInt();
in.nextLine();
// for (int i = 1; i < n + 1; i++) {
// for (int j = 1; j < n + 1; j++) {
// System.out.print(graph[i][j] == Integer.MAX_VALUE ? -1 + " " : graph[i][j] +
// " ");
// }
// System.out.println();
// }
while (q-- != 0) {
String s = in.nextLine();
String[] t = s.split(" ");
int idx1 = idMap.get(t[0]);
int idx2 = idMap.get(t[1]);
int dist = graph[idx1][idx2] == Integer.MAX_VALUE ? -1 : graph[idx1][idx2];
System.out.println(dist);
}
}
}
第四题
没时间了,骗了点分
67%
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = 0;
int m = 0;
ArrayList<int[]> graph = new ArrayList<>();
while(in.hasNext()) {
String s = in.nextLine();
String[] t = s.subSequence(1,s.length()-1).toString().split(", ");
n = t.length;
}
if(n==1)
System.out.println(0);
else
System.out.println(2);
}
}