依图笔试,第一题70,第三题10,不知道为什么!请大佬们看看
// import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } int res = 0; if (n > 1 && arr[1] == 0){ if (arr[0] == 0){ arr[0] = 1; res++; } } int i = 1; for (; i < n-1; i++){ if (arr[i] == 0 && arr[i-1] == 0 && arr[i+1] == 0){ arr[i] = 1; res++; } } if (i == n-1 && arr[i-1] == 0){ res++; } System.out.println(res); } }
第三题
import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] str = sc.nextLine().split(" "); int N = Integer.parseInt(str[0]); int M = Integer.parseInt(str[1]); int K = Integer.parseInt(str[2]); int[][][] arr = new int[K][N][M]; boolean[][] mark = new boolean[N][M]; for (int k = 0; k < K; k++){ for (int n = 0; n < N; n++){ for (int m = 0; m < M; m++){ arr[k][n][m] = sc.nextInt(); } } } for (int k = 0; k < K-1; k++){ compare(arr[k], arr[k+1], mark); } int res = 0; for (int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ if (mark[i][j]) res++; } } if (res == 0) System.out.println(-1); else System.out.println(res); } private static void compare(int[][] a, int[][] b, boolean[][] mark){ int x = a.length; if (x == 0) return; int y = a[0].length; for (int i = 0; i < x; i++){ for (int j = 0; j < y; j++){ if ((a[i][j] ^ b[i][j]) == 1){ mark[i][j] = true; } } } } }