蓝桥杯第十届JavaB组复习

在这里插入图片描述
在这里插入图片描述

  1. 自己数:
    1号位:98 最大,对应17号,往右看,发现17号最大分值是在3号位,所以一号位只能选1号选手——97
    2号位:选10或者20都可以——99
    3号位:17号——99
    4号位:17号已经选了,只能选11或15号——97
    5号位:12或18——98
    sum =972 +992 +98=490
  2. 编程:
    细节一定要注意!!!
    在这里插入图片描述
static int max = 0;
	static int n = 20, m = 6;

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[][] arr = new int[n][m];
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] = scanner.nextInt();
			}
		}
		dfs(new HashSet<>(), arr, 1, 0);// 从1号位开始
		System.out.println(max);
	}

	// 遍历1~20号,20个选5个
	/**
	 * @param set:标记该编号的球手有没有被选
	 * @param arr
	 * @param index:几号位
	 * @param sum:最大的
	 */
	public static void dfs(HashSet<Integer> set, int[][] arr, int index, int sum) {
		max = Math.max(max, sum);
		if (index == 6) {
			return;
		}
		for (int i = 0; i < n; i++) {// 编号1~20都选一遍
			if (!set.contains(i)) {
				set.add(i);
				dfs(set, arr, index + 1, sum + arr[i][index]);
				set.remove(i);
			}

		}

	}

或者暴力

public class Main8 {
	static int max = 0;
	static int n = 20, m = 6;

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[][] arr = new int[n][m];
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] = scanner.nextInt();
			}
		}
		for (int a = 0; a < n; a++) {
			for (int b = 0; b < n; b++) {
				for (int c = 0; c < n; c++) {
					for (int d = 0; d < n; d++) {
						for (int e = 0; e < n; e++) {
							int temp = 0;
							if (a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e
									&& d != e) {
								temp = arr[a][1] + arr[b][2] + arr[c][3] + arr[d][4] + arr[e][5];
								max = Math.max(max, temp);
							}
						}
					}
				}
			}
		}
		System.out.println(max);
	}
}

在这里插入图片描述

public static void main(String[] args) {
		HashSet<String> set = new HashSet<>();
		String string = "0100110001010001";
		// String string = "aaab";
		for (int i = 0; i < string.length(); i++) {
			for (int j = i + 1; j <= string.length(); j++) {
				String temp = string.substring(i, j);//j从i+1 开始,保证最小的长度为1,满足题意
				set.add(temp);
			}
		}
		// for (String s : set) {
		// System.out.println(s);
		// }
		System.out.println(set.size());
	}

在这里插入图片描述


在这里插入图片描述

public static void main(String[] args) {
		// 最后4位,20190324,可能发生溢出
		long a = 1, b = 1, c = 1;
		long d = 4;
		while (d <= 20190324) {
			// 最后1位,%10
			// 最后2位:%100
			// 最后3位:%1000
			// 最后4位:%10000
			long sum = (a + b + c) % 10000;// 要求最后4位,%10000
			a = b;
			b = c;
			c = sum;
			d++;
		}
		System.out.println(c);
	}

在这里插入图片描述


在这里插入图片描述

public static void main(String[] args) {
		// 2019分解成3个各不相同的正整数
		// 正整数:>=0
		HashSet<List<Integer>> set = new HashSet<>();
		for (int i = 1; i <= 2019; i++) {
			for (int j = 1; j <= 2019; j++) {
				int k = 2019 - i - j;
				if (k >= 1) {
					String temp = i + "" + j + "" + k;
					if (!temp.contains("2") && !temp.contains("4")) {
						List<Integer> list = new ArrayList<Integer>();
						list.add(i);
						list.add(j);
						list.add(k);
						Collections.sort(list);
						set.add(list);
					}
				}
			}
		}
		for (List<Integer> list : set)
			System.out.println(list);
		System.out.println(set.size());
	}

在这里插入图片描述
最好还是打印出来看一下,我刚才就犯了错误,忘记判断k是否>=1,造成有负数的情况,一定要注意细节!

对不起!!!!!!!!!!!!!!我写错了!!我是瓜娃子
我忘记判断三个数不一样了
敲黑板,重点!!!

if (k >= 1 && i != j && i != k && j != k)

public static void main(String[] args) {
		// 2019分解成3个各不相同的正整数
		// 正整数:>=0
		HashSet<List<Integer>> set = new HashSet<>();
		for (int i = 1; i <= 2019; i++) {
			for (int j = 1; j <= 2019; j++) {
				int k = 2019 - i - j;
				if (k >= 1 && i != j && i != k && j != k) {
					String temp = i + "" + j + "" + k;
					if (!temp.contains("2") && !temp.contains("4")) {
						List<Integer> list = new ArrayList<Integer>();
						list.add(i);
						list.add(j);
						list.add(k);
						Collections.sort(list);
						set.add(list);
					}
				}
			}
		}
		System.out.println(set.size());
	}

正确答案:40785
在这里插入图片描述


在这里插入图片描述

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
public class Main5 {
	// 迷宫这道题因为要求步数最少且字典序最小,采用bfs做
	// static int n = 4, m = 6;
	static int n = 30, m = 50;
	static char[][] grid = new char[n][m];
	static boolean[][] flag = new boolean[n][m];

	public static void main(String[] args) throws FileNotFoundException {
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < n; i++) {// 存储数据
			grid[i] = scanner.nextLine().toCharArray();
		}
		Queue<Node> queue = new LinkedList<>();// 辅助队列
		queue.add(new Node(0, 0, '.', null));// 加入左上角,其父节点设为null
		while (!queue.isEmpty()) {
			Node temp = queue.poll();
			int x = temp.x;
			int y = temp.y;
			if (x == n - 1 && y == m - 1) {// 走到右下角
				StringBuilder builder = new StringBuilder();// 用于记录路径
				while (temp.parent != null) {// 倒序输出结点
					builder.insert(0, temp.c);// 倒着往回走
					temp = temp.parent;
				}
				System.setOut(new PrintStream((new File("迷宫.txt"))));//把下方的输出转为输出到文件
				System.out.println(builder.toString());
				continue;//最好改成continue
			}
			// 向下
			if (x >= 0 && y >= 0 && x + 1 < n && y < m && grid[x + 1][y] == '0' && !flag[x + 1][y]) {
				queue.add(new Node(x + 1, y, 'D', temp));
				flag[x + 1][y] = true;
			}
			// 向左
			if (x >= 0 && y - 1 >= 0 && x < n && y < m && grid[x][y - 1] == '0' && !flag[x][y - 1]) {
				queue.add(new Node(x, y - 1, 'L', temp));
				flag[x][y - 1] = true;
			}
			// 向右
			if (x >= 0 && y >= 0 && x < n && y + 1 < m && grid[x][y + 1] == '0' && !flag[x][y + 1]) {
				queue.add(new Node(x, y + 1, 'R', temp));
				flag[x][y + 1] = true;
			}
			// 向上
			if (x - 1 >= 0 && y >= 0 && x < n && y < m && grid[x - 1][y] == '0' && !flag[x - 1][y]) {
				queue.add(new Node(x - 1, y, 'U', temp));
				flag[x - 1][y] = true;
			}
		}
	}

	static class Node {
		int x, y;// 该节点的坐标
		char c;// 符合
		Node parent;// 父节点

		public Node() {
		}

		public Node(int x, int y, char c, Node parent) {
			super();
			this.x = x;
			this.y = y;
			this.c = c;
			this.parent = parent;
		}
	}
}

在这里插入图片描述

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

在这里插入图片描述
在这里插入图片描述

public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		long sum = 0;
		for (int i = 1; i <= n; i++) {
			String temp = i + "";
			if (temp.contains("2") || temp.contains("0") || temp.contains("1") || temp.contains("9")) {
				sum += i;
			}
		}
		System.out.println(sum);
	}

在这里插入图片描述
在这里插入图片描述

public class Main7 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();// 1~N
		int M = scanner.nextInt();// M条信息
		int T = scanner.nextInt();// 时刻
		int[][] a = new int[M][2];// 存储信息
		for (int i = 0; i < M; i++) {
			a[i][0] = scanner.nextInt();// ts
			a[i][1] = scanner.nextInt();// id
		}
		// 按时间排个序好一点
		Arrays.sort(a, new Comparator<int[]>() {

			@Override
			public int compare(int[] o1, int[] o2) {
				if (o1[0] == o2[0])
					return o1[1] - o2[1];
				return o1[0] - o2[0];
			}
		});
		int[] pri = new int[N];// 对应1~N家外卖店,初始时为0
		boolean[] isPri = new boolean[N];// 以true和false来表示是否加入该店面
		int time = 1;// 从1开始
		while (time <= T) {
			HashSet<Integer> set = new HashSet<>();// 存储每一轮有订单的店面
			for (int i = 0; i < a.length; i++) {// 遍历数组,查找是否有对应ts
				int temp = a[i][0];
				int id = a[i][1] - 1;// 调bug才发现的错误!
				if (temp != time) {
					continue;
				}
				// 因为同一个时刻,有订单的优先级+2,没有订单的优先级-1
				// 可以用set来存储
				set.add(id);// 有订单就加入
				pri[id] += 2;// 存在ts,给对应id的优先级+2
			}
			// 遍历完了之后,该加的优先级已经加了,现在要进行没有订单的优先级处理以及是否加入缓存
			for (int id = 0; id < N; id++) {
				if (!set.contains(id)) {// set里面有该id,说明此时刻有订单,没有则说明没有订单
					pri[id] = Math.max(pri[id] - 1, 0);// 最小为0,如果之前就是0,pri-1之后为负数
				}
				if (pri[id] > 5) {// 加入优先缓存
					isPri[id] = true;
				} else if (pri[id] <= 3) {
					isPri[id] = false;
				}
			}
			time++;
		}
		// K个时刻之后,看ispri里为true的有多少个
		int cnt = 0;
		for (int i = 0; i < isPri.length; i++) {
			cnt += isPri[i] ? 1 : 0;
		}
		System.out.println(cnt);
	}
}

后面的太难了,不会做

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值