荣耀笔试4/22
第一次ak 记录一下
第一题
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer> arrayList = new ArrayList<>();
while (in.hasNextInt()) {
int a = in.nextInt();
arrayList.add(a);
if (a == -1) break;
}
int[] num = new int[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
num[i] = arrayList.get(i);
}
int[] dp = new int[num.length];
Arrays.fill(dp, Integer.MAX_VALUE - 1);
dp[0] = 0;
for (int i = 1; i < num.length / 2; i++) {
dp[i] = 1;
}
for (int i = 1; i < num.length; i++) {
if (i + num[i] < num.length) dp[i + num[i]] = Math.min(dp[i + num[i]], dp[i] + 1);
}
if (dp[dp.length-1]==Integer.MAX_VALUE-1){
System.out.println(-1);
}else {
System.out.println(dp[dp.length - 1]);
}
}
}
第二题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] res = new int[n][n];
int start = 0, count = 1;
int i, j, temp = 0;
while (temp < n / 2) {
temp++;
for (j = start; j < n - temp; j++) {
res[j][n - 1 - start] = count++;
}
for (i = start; i < n - temp; i++) {
res[j][n - 1 - i] = count++;
}
for (j = j; j >= temp; j--) {
res[j][n - 1 - i] = count++;
}
for (i = i; i >= temp; i--) {
res[j][n - 1 - i] = count++;
}
start++;
}
if (n % 2 == 1) {
res[n / 2][n / 2] = count;
}
for (int[] re : res) {
for (int l = 0; l < res[0].length; l++) {
if (String.valueOf(re[l]).length() == 1) {
System.out.print(" " + re[l]);
} else {
System.out.print(" " + re[l]);
}
}
System.out.println();
}
}
}
第三题
import java.util.*;
class Point {
int x;
int y;
int height;
public Point(int x, int y, int height) {
this.x = x;
this.y = y;
this.height = height;
}
}
public class Main {
static int n;
static int m;
static int[][] direction = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Point> list = new ArrayList<>();
n = sc.nextInt();
m = sc.nextInt();
int[][] graph = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
list.add(new Point(i, j, graph[i][j]));
}
}
list.sort(Comparator.comparing(c -> c.height));
int xA = sc.nextInt();
int yA = sc.nextInt();
int xB = sc.nextInt();
int yB = sc.nextInt();
long[][] dp = new long[n][m];
dp[xA][yA] = 1;
for (Point point : list) {
for (int[] d : direction) {
int x = point.x + d[0];
int y = point.y + d[1];
if (ifValid(x, y) && graph[x][y] < graph[point.x][point.y]) {
dp[point.x][point.y] = (dp[point.x][point.y] % 1000000000 + dp[x][y] % 1000000000) % 1000000000;
}
}
}
System.out.println(dp[xB][yB] % 1000000000);
}
public static boolean ifValid(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < m;
}
}