蓝桥杯31天冲刺打卡题解(Day4)

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} 3i5j7k 有多少值小于 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;
        }
    }
}
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小成同学_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值