2019年第十届蓝桥杯省赛 Java B 组真题
第一题
第二题
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
第三题
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
第四题
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
第五题
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
第六题
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;
}
}
第七题
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);
}
}
第八题
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;
}
}
}
第九题
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);
}
}
}