2019年第十届蓝桥杯省赛 Java B 组真题

2019年第十届蓝桥杯省赛 Java B 组真题

第一题

截屏2021-04-05 19.55.46 截屏2021-04-05 19.56.00 截屏2021-04-05 19.57.14

第二题

截屏2021-04-05 20.00.50
import java.util.HashSet;
import java.util.Set;

public class Q2 {

	static String s = "0100110001010001";
	static Set<String> set = new HashSet<>();
	
	public static void main(String[] args) {
		for (int i = 0; i < s.length(); i++) {
			for (int j = i + 1; j < s.length() + 1; j++) {
				set.add(s.substring(i, j));
			}
		}
		System.out.println(set.size());
	}
}

答案:100

第三题

截屏2021-04-05 20.04.12
public class Q3 {

	static int mod = 10000;
	static int a, b, c, d;
	
	public static void main(String[] args) {
		a = b = c = 1;
		for (int i = 4; i <= 20190324; i++) {
			d = (a + b + c) % mod;
			a = b;
			b = c;
			c = d;
		}
		System.out.println(d);
	}
}

答案:4659

第四题

截屏2021-04-05 20.07.36
public class Q4 {

	public static void main(String[] args) {
		int ans = 0;
		for (int i = 1; i < 2019; i++) {
			if (!check(i)) continue;
			for (int j = i + 1; j < 2019; j++) {
				int k = 2019 - i - j;
				if (k > j && check(k) && check(j))
					ans++;
			}
		}
		System.out.println(ans);
	}
	
	static boolean check(int num) {
		while (num > 0) {
			int x = num % 10;
			if (x == 2 || x == 4)
				return false;
			num /= 10;
		}
		return true;
	}
}

答案:40785

第五题

截屏2021-04-05 20.14.40
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Q5 {

	static int n = 30, m = 50;
	static int[][] mp = new int[n][m];
	static boolean[][] vis = new boolean[n][m];
	static int[][] path = new int[n][m];
	static char[] pp = {'D', 'L', 'R', 'U'};
	static int[][] delta = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};
	
	public static void main(String[] args) throws IOException{
		Scanner sc = new Scanner(new File("maze.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] - '0';
			}
		}
		
		bfs();
		
		StringBuilder sb = new StringBuilder();
		int x = n - 1, y = m - 1, i;
		while (x != 0 || y != 0) {
			i = path[x][y];
			sb.append(pp[i]);
			x -= delta[i][0];
			y -= delta[i][1];
		}
		
		FileWriter fw = new FileWriter("out.txt");
		fw.write(sb.reverse().toString());
		fw.close();
	}
	
	static void bfs() {
		Queue<int[]> queue = new LinkedList<int[]>();
		queue.add(new int[] {0, 0});
		vis[0][0] = true;
		
		while (!queue.isEmpty()) {
			int[] cur = queue.poll();
			if (cur[0] == n - 1 && cur[1] == n - 1 ) 
				break;
			for (int i = 0; i < delta.length; i++) {
				int xx = cur[0] + delta[i][0];
				int yy = cur[1] + delta[i][1];
				if (xx >= 0 && xx < n && yy >= 0 && yy < m && mp[xx][yy] != 1 && !vis[xx][yy]) {
					queue.add(new int[] {xx, yy});
					vis[xx][yy] = true;
					path[xx][yy] = i;
				}
			}
		}
	}
}

答案:DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

第六题

截屏2021-04-05 20.48.25
import java.util.Scanner;

public class Q6 {

	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++) {
			if (check(i)) {
				ans += i;
			}
		}
		System.out.println(ans);
	}
	
	static boolean check(int num) {
		while (num > 0) {
			int x = num % 10;
			if (x == 2 || x == 0 || x == 1 || x == 9)
				return true;
			num /= 10;
		}
		return false;
	}
}

第七题

截屏2021-04-05 20.54.29 截屏2021-04-05 20.54.46
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Q7 {

	static int MAX_N = 100005;
	static List<Integer>[] order = new ArrayList[MAX_N];
	static int n, m, t, ts, id, cur, pre, priority, ans = 0;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		t = sc.nextInt();
		for (int i = 1; i <= n; i++)
			order[i] = new ArrayList<>();
		while (m-- > 0) {
			ts = sc.nextInt();
			id = sc.nextInt();
			order[id].add(ts);
		}
		
		for (int i = 1; i <= n; i++) {
			if (order[i].size() < 3) continue;
			Collections.sort(order[i]);
			priority = 2;
			pre = order[i].get(0);
			for (int j = 1; j < order[i].size(); j++) {
				cur = order[i].get(j);
				if (cur - pre <= 1)
					priority += 2;
				else 
					priority = Math.max(priority - (cur - pre - 1), 0) + 2;
				pre = cur;
			}
			if (pre < t)
				priority = Math.max(priority - (t - pre), 0);
			
			if (priority < 4) continue; // 小于等于 3,移出优先级队列
			// 6 -> 5 -> 4 √
			// 3 -> 5 -> 4 x
			// 2 -> 4 x
			if (priority == 4 && t - pre <= 1) continue; 
			// 6 -> 5 √
			// 3 -> 5 x
			if (priority == 5 && pre == t) continue;
			ans++;
		}
		
		System.out.println(ans);
	}
}

第八题

截屏2021-04-06 08.40.29 截屏2021-04-06 08.40.49
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Q8 {

	static String str;
	static String s1 = "Alice";
	static String s2 = "Bob";
	static List<Integer> l1, l2;
	static int k;
	static long ans = 0;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		k = sc.nextInt();
		sc.nextLine();
		str = sc.nextLine();
		l1 = f(s1);
		l2 = f(s2);
		
		int left = 0, right = 0;
		for (int i = 0; i < l1.size(); i++) {
			while (left < l2.size() && l1.get(i) - l2.get(left) - 3 > k) {
				left++;
			}
			while (right < l2.size() && l2.get(right) - l1.get(i) - 5 <= k) {
				right++;
			} 
			ans += right - left;
		}
		
		System.out.println(ans);
	}
	
	static List<Integer> f(String s) {
		int index = 0;
		List<Integer> list = new ArrayList<>();
		while (true) {
			index = str.indexOf(s, index);
			if (index == - 1)
				return list;
			if ((index == 0 || !Character.isAlphabetic(str.charAt(index - 1)))
					&& (index + s.length() == str.length() || !Character.isAlphabetic(str.charAt(index + s.length()))))
				list.add(index);
			index += 1;
		}
	}
}

第九题

截屏2021-04-06 10.32.52
import java.util.Arrays;
import java.util.Scanner;

public class Q9 {

	static int MAX_N = 100005;
	static int[] a = new int[MAX_N * 2];
	static int n, m;
	static long sum = 0;	
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		for (int i = 0; i < n + m + 1; i++) {
			a[i] = sc.nextInt();
		}
		
		Arrays.sort(a, 0, n + m + 1);
		int cnt = 0;
		for (int i = 0; i < n + m + 1; i++) {
			sum += a[i];
			if (a[i] < 0) 
				cnt++;
		}
		
		if (m == 0) {
			System.out.println(sum);
		} else {
			if (cnt > 0) {
				if (cnt != n + m + 1) {
					for (int i = 0; i < cnt; i++)
						sum -= 2 * a[i];
				} else {
					for (int i = 0; i < cnt - 1; i++)
						sum -= 2 * a[i];
				}
			} else {
				sum -= 2 * a[0];
			}
			System.out.println(sum);
		}
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值