【蓝桥杯研究生组】第14届Java试题答案整理
试题链接:链接
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) {// 判断最后的输出值是否为1return 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; // 返回满足条件的组合数量}
}