Day4
第一题
第十届2019年蓝桥杯国赛
奇数倍数
JavaC组第1题
填空题
直接暴力枚举,依旧是循环标签的使用。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
outer:
for (int i = 2019; ; i += 2019) { // i必定是2019的倍数,所以循环条件每次可以直接+2019
int x = i;
while (x > 0) {
if (x % 10 % 2 == 0) continue outer; // 回到最外层循环
x /= 10;
}
System.out.print(i);
break;
}
}
}
第二题
第九届2018年蓝桥杯省赛
第几个幸运数字
JavaC组第4题
填空题
同样暴力枚举,三层循环分别枚举3、5、7的倍数,求 3 i ⋅ 5 j ⋅ 7 k 3^{i}·5^{j}·7^{k} 3i⋅5j⋅7k 有多少值小于 59084709587505 59084709587505 59084709587505 。
public class Main {
public static void main(String[] args) {
long n = 59084709587505l;
int ans = 0;
for (int i = 0; Math.pow(3, i) < n; i++) // 枚举3的倍数
for (int j = 0; Math.pow(5, j) < n; j++) // 枚举5的倍数
for (int k = 0; Math.pow(7, k) < n; k++) // 枚举7的倍数
if (Math.pow(3, i) * Math.pow(5, j) * Math.pow(7, k) < n) // 满足3^i x 5^j × 7^k的有几个数
ans++;
System.out.println(ans);
}
}
第三题
第七届2016年蓝桥杯省赛
四平方和
之前用二分写过这题的题解,可参考这篇:蓝桥杯AcWing学习笔记 2-1二分的学习
题解写的很详细,三种代码:三层循环暴搜(超时)、用类存值二分(超时)、用集合存值二分(AC),还有蓝桥评测的分数。
第四题(选做)
第十届2019年蓝桥杯国赛
迷宫
JavaB组第5题
填空题
看到最小步数,又是走迷宫,自然就想到用bfs,当然题中还有要求路径的字典序最小,所以在搜索的时候我们就可以按照字典序来搜索。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static final int N = 30, M = 50;
static char[][] map = new char[N][M]; // 存迷宫
static boolean[][] st = new boolean[N][M]; // 记录是否遍历过 false未遍历 true已遍历
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < N; i++) map[i] = sc.next().toCharArray();
bfs();
}
private static void bfs() {
Queue<Point> q = new LinkedList<>();
q.offer(new Point(0, 0, ""));
st[0][0] = true;
int[] dx = {1, 0, 0, -1}, dy = {0, -1, 1, 0}; // 存偏移量
char[] c = {'D', 'L', 'R', 'U'};
while (!q.isEmpty()) {
Point t = q.poll();
if (t.x == N - 1 && t.y == M - 1) {
System.out.print(t.s);
}
for (int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= N || y < 0 || y >= M) continue; // 出界
if (map[x][y] == '0' && !st[x][y]) {
q.offer(new Point(x, y, t.s + c[i]));
st[x][y] = true;
}
}
}
}
static class Point {
int x;
int y;
String s; // D、U、L、R
public Point(int x, int y, String s) {
this.x = x;
this.y = y;
this.s = s;
}
}
}