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

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

第一题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第1张图片
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

第二题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第2张图片
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);
	}
}

第三题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第3张图片
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

第四题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第4张图片
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

第六题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第5张图片
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));
	}
}

第七题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第6张图片 2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第7张图片
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);
	}
}

第八题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第8张图片 2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第9张图片
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);
	}
}

第九题

2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第10张图片 2020年第十一届蓝桥杯第二场参赛 Java B 组真题_第11张图片
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);
	}
}

你可能感兴趣的:(蓝桥杯,算法,动态规划,图论)