2020年第十一届蓝桥杯第二场参赛 Java B 组真题

本文解析了2020年蓝桥杯JavaB组的四道题目,涉及计数、矩阵操作、数独求解、路径最大和与字符出现频率等技术,详细介绍了每道题目的解法和答案:Q1计数2的个数624;Q2矩阵匹配2020出现次数80;Q3数独求特定位置数值761;Q4图论应用80。
摘要由CSDN通过智能技术生成

2020年第十一届蓝桥杯第二场参赛 Java B 组真题

第一题

截屏2021-04-07 13.56.31
public class Q1 {

	public static void main(String[] args) {
		int sum = 0;
		for (int i = 1; i <= 2020; i++) {
			sum += count(i);
		}
		System.out.println(sum);
	}
	
	static int count(int num) {
		int res = 0;
		while (num > 0) {
			int x = num % 10;
			if (x == 2)
				res++;
			num /= 10;
		}
		return res;
	}
}

答案:624

第二题

截屏2021-04-07 14.00.27
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Q2 {
	
	static int n = 6;
	static int m = 6;
	static char[][] mp = new char[6][6];
	static String str;
	static String target = "2020";
	static int ans = 0;

	public static void main(String[] args) throws FileNotFoundException {
//		Scanner sc = new Scanner(System.in);
		Scanner sc = new Scanner(new File("2020.txt"));
		for (int i = 0; i < n; i++) {
			char[] temp = sc.nextLine().toCharArray();
			for (int j = 0; j < m; j++) {
				mp[i][j] = temp[j];
			}
		}
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (mp[i][j] == '2') { 
					if (j + 3 < m) {
						str = mp[i][j] + "" + mp[i][j+1] + "" + mp[i][j+2] + "" + mp[i][j+3];
						if (target.equals(str)) 
							ans++;
					}
					if (i + 3 < n) {
						str = mp[i][j] + "" + mp[i+1][j] + "" + mp[i+2][j] + "" + mp[i+3][j];
						if (target.equals(str))
							ans++;
					}
					if (i + 3 < n && j + 3 < m) {
						str = mp[i][j] + "" + mp[i+1][j+1] + "" + mp[i+2][j+2] + "" + mp[i+3][j+3];
						if (target.equals(str))
							ans++;
					}
				}
			}
		}
		
		System.out.println(ans);
	}
}

第三题

截屏2021-04-07 14.11.57
public class Q3 {

	static int MAX_N = 50;
	static int[][] mp = new int[MAX_N][MAX_N];
	static int cnt = 1;
	
	public static void main(String[] args) {
		for (int i = 1; i < MAX_N; i++) {
			// 奇数行从 (i,1) 到 (1, i)
			if (i % 2 != 0) {
				for (int j = 0; j < i; j++) {
					mp[i - j][1 + j] = cnt++;
				}
			} else {
				for (int j =0; j < i; j++) {
					mp[1 + j][i - j] = cnt++;
				}
			}
		}
		
		System.out.println(mp[20][20]);
	}
}

答案:761

第四题

截屏2021-04-07 14.18.43
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Q4 {

	static boolean[] book = new boolean[7];
	static boolean[][] graph = new boolean[7][7];
	static {
		graph[0][1] = graph[0][5] = true;
		graph[1][0] = graph[1][2] = graph[1][6] = true;
		graph[2][1] = graph[2][3] = graph[2][6] = true;
		graph[3][2] = graph[3][4] = true;
		graph[4][3] = graph[4][5] = graph[4][6] = true;
		graph[5][0] = graph[5][4] = graph[5][6] = true;
		graph[6][1] = graph[6][2] = graph[6][4] = graph[6][5] = true;
	}
	static int ans = 0;
	
	public static void main(String[] args) {
		dfs(0);
		System.out.println(ans);
	}
	
	static void judge() {
		int cnt = 0;
		boolean[] vis = new boolean[7];
		Arrays.fill(vis, true);
		Queue<Integer> queue = new LinkedList<>();
		for (int i = 0; i < 7; i++) {
			if (book[i]) {
				vis[i] = false;
				cnt++;
				if (queue.isEmpty()) {
					queue.add(i);
					vis[i] = true;
				}
			}
		}
		
		if (queue.isEmpty()) return;
		
		while (!queue.isEmpty()) {
			int cur = queue.poll();
			cnt--;
			for (int i = 0; i < 7; i++) {
				if (graph[cur][i] && !vis[i]) {
					queue.add(i);
					vis[i] = true;
				}
			}
		}
		
		if (cnt == 0) {
			for (int i = 0; i < 7; i++)
				if (book[i])
					System.out.print((char)(i + 'a'));
			System.out.println();
			ans++;
		}
	}
	
	static void dfs(int k) {
		if (k == 7) {
			judge();
			return;
		}
		book[k] = true;
		dfs(k + 1);
		book[k] = false;
		dfs(k + 1);
	}
}

答案:80

第六题

截屏2021-04-07 14.56.29 截屏2021-04-07 14.57.18
import java.util.Scanner;

public class Q6 {

	static int n, max = 0, min = 100, sum = 0;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			min = Math.min(min, x);
			max = Math.max(max, x);
			sum += x;
		}
		System.out.println(max);
		System.out.println(min);
		System.out.printf("%.2f", 0.01 * Math.round(100.0 * sum / n));
	}
}

第七题

截屏2021-04-07 15.04.01 截屏2021-04-07 15.07.19
import java.util.Scanner;

public class Q7 {

	static int[] count = new int[128];
	static String str;
	static int max;
	static char ans;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		str = sc.next();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			count[c]++;
			if (count[c] > max) {
				max = count[c];
				ans = c;
			} 
			if (count[c] == max && c < ans) {
				ans = c;
			}
		}
		
		System.out.println(ans);
		System.out.println(max);
	}
}

第八题

截屏2021-04-07 15.12.50 截屏2021-04-07 15.13.20
import java.util.Arrays;
import java.util.Scanner;

public class Q8 {

	static int MAX_N = 105;
	static int[][] mp = new int[MAX_N][MAX_N];
    // dp[i][j][k]: 走到位置(i,j),向左走了 k 步时的最大和
	static int[][][] dp = new int[MAX_N][MAX_N][MAX_N];
	static int n, ans = 0;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				mp[i][j] = sc.nextInt();
			}
		}
		
		for (int i = 0; i < MAX_N; i++) {
			for (int j = 0; j < MAX_N; j++) {
				Arrays.fill(dp[i][j], -100000);
			}
		}
		
		dp[1][1][0] = mp[1][1];
		for (int i = 2; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				for (int k = 1; k < i; k++) {
					dp[i][j][k] = Math.max(dp[i - 1][j][k - 1], dp[i - 1][j - 1][k]) + mp[i][j];
				}
				dp[i][j][0] = dp[i - 1][j - 1][0] + mp[i][j];
			}
		}
		
		for (int j = 1; j <= n; j++) {
			if (n % 2 == 0) {
				ans = Math.max(ans, Math.max(dp[n][j][n / 2 - 1], dp[n][j][n / 2]));
			} else {
				ans = Math.max(ans, dp[n][j][n / 2]);
			}
		}
		
		System.out.println(ans);
	}
}

第九题

截屏2021-04-07 16.27.35 截屏2021-04-07 16.28.01
import java.util.Scanner;

public class Q8 {
	
	static char[] s;
	static int[] pre = new int[128];
	static int n;
 	static long ans = 0;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		s = sc.next().toCharArray();
		n = s.length;
		for (int i = 1; i <= n; i++) {
			char c = s[i - 1];
			ans += (long)(n - i + 1) * (long)(i - pre[c]);
			pre[c] = i;
		}
		System.out.println(ans);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值