试题链接:链接
A题
满足条件的答案有:35813116
public class TianShu {
public static void main(String[] args) {
int ans = 0;
// 2000.1.1 - 2000000.1.1
// 年份是月份的倍数,也是日的倍数
for (int year=2000; year<=2000000; year++) {
for (int month=1; month<=12; month++) {
if (month == 2) {
for (int day=1; day<=28; day++) {
if (year % month == 0 && year % day == 0) {
ans++;
}
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { // 闰年补一个2月29日
if (year % month == 0 && year % 29 == 0) {
ans++;
}
}
} else if (month == 1 || month ==3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
for (int day=1; day<=31; day++) {
if (year % month == 0 && year % day == 0) {
ans++;
}
}
} else {
for (int day=1; day<=30; day++) {
if (year % month == 0 && year % day == 0) {
ans++;
}
}
}
}
}
System.out.println("满足条件的答案有:" + ans);
}
}
B题
public class LogicGateSimulation {
public static void main(String[] args) {
// 定义输入数组
int[] input = {1, 0, 1, 0, 1};
// 数组用于储存中间结果
int[][] arr = new int[5][5];
// 初始化输入
for (int i = 0; i < 5; i++) {
arr[0][i] = input[i];
}
// 计算每一层的结果,并统计满足条件的组合数量
int count = calculateGatesHelper(arr, 1);
System.out.println("满足条件的组合数量: " + count);
}
// 参数含义:
// arr 用于存储中间结果的数组
// layer 当前处理的层数
private static int calculateGatesHelper(int[][] arr, int layer) {
// 递归终止条件:到达最后一层
if (layer == arr.length) {
// 判断最后的输出值是否为1
return arr[layer - 1][0] == 1 ? 1 : 0;
}
int totalCombinations = 0; // 用于统计满足条件的组合数量
// 遍历所有可能的逻辑门(AND、OR、XOR)
for (int gate = 0; gate < 3; gate++) {
// 遍历当前层的所有列数(有效列数为 5 - layer)
for (int i = 0; i < (5 - layer); i++) {
// 根据逻辑门类型计算
switch (gate) {
case 0: // AND 门
arr[layer][i] = arr[layer - 1][i] & arr[layer - 1][i + 1];
break;
case 1: // OR 门
arr[layer][i] = arr[layer - 1][i] | arr[layer - 1][i + 1];
break;
case 2: // XOR 门
arr[layer][i] = arr[layer - 1][i] ^ arr[layer - 1][i + 1];
break;
}
}
// 递归处理下一层
totalCombinations += calculateGatesHelper(arr, layer + 1);
}
return totalCombinations; // 返回满足条件的组合数量
}
}
C题
import java.util.Arrays;
import java.util.Scanner;
public class QiPan {
public static void main(String[] args) {
// 二维数组操作
Scanner scanner = new Scanner(System.in);
// 棋盘大小 和 操作数
int len = scanner.nextInt();
int[][] qipan = new int[len][len];
int operationNum = scanner.nextInt();
// 操作
for (int i=0; i<operationNum; i++) {
int x1 = scanner.nextInt() - 1;
int y1 = scanner.nextInt() - 1;
int x2 = scanner.nextInt() - 1;
int y2 = scanner.nextInt() - 1;
// 做操作
for (int j = x1; j <= x2; j++) {
for (int k = y1; k <=y2; k++) {
qipan[j][k] = qipan[j][k] == 0 ? 1 : 0; // 取反
}
}
}
for (int i=0; i<len; i++) {
for (int j=0; j<len; j++) {
System.out.print(qipan[i][j]);
}
System.out.println();
}
}
}
D题
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import java.util.Scanner;
public class ZiJuZhen {
public static void main(String[] args) {
int modNum = 998244353;
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int[][] matrix = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
int ans = 0;
// 方法一 暴力穷举所有 a * b 的子数组
for (int i=0; i <= (m - a); i++) {
for (int j=0; j <= (n - b); j++) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int k=0; k < a; k++) {
for (int q=0; q < b; q++) {
min = Math.min(min, matrix[i+k][j+q]);
max = Math.max(max, matrix[i+k][j+q]);
}
}
ans += min*max;
}
}
System.out.println("价值为 :" + ans%modNum);
}
}