蓝桥杯打卡-Day4

酶和ATP 2022年03月11日 883次浏览

奇数倍数-2019国赛

https://www.lanqiao.cn/problems/818/learning/

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

请你找到最小的整数 X 同时满足:

  1. X 是 2019 的整倍数;
  2. X 的每一位数字都是奇数。

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

暴力解决,答案是139311.

#include <iostream>
using namespace std;

bool check(int x) {
    while (x) {
        if (x % 2 == 0) return false;
        x /= 10;
    }
    return true;
}

int main() {
    for (int i = 2019; i;) {
        if (check(i)) {
            cout << i << endl;
            break;
        }
        i += 2019;
    }
    return 0;
}

第几个幸运数字-2018省赛

https://www.lanqiao.cn/problems/613/learning/

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

到 X 星球旅行的游客都被发给一个整数,作为游客编号。

X 星的国王有个怪癖,他只喜欢数字 3,5 和 7。

国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。

我们来看前 10 个幸运数字是:

3 5 7 9 15 21 25 27 35 45

因而第 11 个幸运数字是: 49

小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。

请你帮小明计算一下,59084709587505 是第几个幸运数字。

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

这个题目只能顺着它的意思,从低到高次分别求3 5 7到乘方,再相乘,用来组成一个幸运数字
而不能反过来枚举 1 - N ,分别对 3/5/7 求余···。这是为什么呢?
提示-有个东西叫质数。

答案是1905

#include <cmath>
#include <iostream>
using namespace std;

typedef long long LL;
const LL N = 59084709587505;
LL cnt;

int main() {
    for (int i = 0; pow(3, i) <= N; i++)
        for (int j = 0; pow(5, j) <= N; j++)
            for (int k = 0; pow(7, k) <= N; k++)
                if (pow(3, i) * pow(5, j) * pow(7, k) <= N) cnt++;
    cout << cnt - 1 << endl;
    return 0;
}

四平方和-2016省赛

http://lx.lanqiao.cn/problem.page?gpid=T2766
四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。

比如:

(^符号表示乘方的意思)

对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法

程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开

例如,输入:
5
则程序应该输出:
0 0 1 2

再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

#include <iostream>
#include <unordered_map>
#define x first
#define y second
using namespace std;

typedef pair<int, int> PII;
const int N = 5000000;
int n;
unordered_map<int, PII> m;
int a, b, c, d;

int main() {
    cin >> n;
    // 首先枚举后面两个大的c d
    for (int c = 0; c * c <= n; c++)
        for (int d = c; c * c + d * d <= n; d++) {
            int res = c * c + d * d;
            m[res] = {c, d};
        }
    
    // 再枚举前面两个
    for (int a = 0; a * a <= n; a++)
        for (int b = a; a * a + b * b <= n; b++) {
            int cd = n - a * a - b * b;
            if (m.find(cd) != m.end()) {
                cout << a << ' ' << b << ' ' << m[cd].x << ' ' << m[cd].y
                     << endl;
                return 0;
            }
        }

    return 0;
}

迷宫-2019省赛

https://www.lanqiao.cn/problems/602/learning/

题目描述

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。

010000
000100
001001
110000

迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。

对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。

请注意在字典序中 D<L<R<U。

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

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 256M

找路bfs_find写出来了,但是回溯输出路径改了半天也不对...
先这样吧,有空了再改。

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 60;
char m[N][N];
bool visited[N][N];
int dist[N][N];

int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
char dists[4] = {'L', 'D', 'U', 'R'};
vector<char> road;

// 这是找路的 bfs
void bfs_find(int x, int y) {
    queue<PII> q;
    q.push({x, y});
    dist[x][y] = 0;
    while (!q.empty()) {
        auto u = q.front();
        visited[u.x][u.y] = true;
        for (int i = 0; i < 4; i++) {
            int ax = u.x + dx[i], ay = u.y + dy[i];
            if (visited[ax][ay]) continue;
            // 如果越界
            if (ax < 0 || ax > 3 || ay < 0 || ay > 5) continue;
            // 只要不是路,就退出
            if (m[ax][ay] != '0') continue;
            // 再加入之前判重
            if (dist[x][y] != 0) continue;
            // 路
            dist[ax][ay] = dist[u.x][u.y] + 1;
            // 如果到了,就退出
            if (ax == 3 && ay == 5) return;
            q.push({ax, ay});
        }
        q.pop();
    }
    return;
}

// 根据找路得到的dist,从迷宫出口回溯找周围dist - 1的格子,把路径放在vector里
void bfs_road(int x, int y) {
    queue<PII> q;
    q.push({x, y});
    while (!q.empty()) {
        auto u = q.front();
        for (int i = 0; i < 4; i++) {
            // 计算地址
            int x = u.x + dx[i], y = u.y + dy[i];
            // 如果找到了dist-1
            if (dist[x][y] == dist[u.x][u.y] - 1) {
                // 就把这个对应的符号加进去
                road.push_back(dists[i]);
                // 入列
                q.push({x, y});
                // 不用找了,因为旁边肯定只有一个dist - 1的点
                break;
            }
            // 如果到了,就退出
            if (x == 0 && y == 0) return;
        }
        q.pop();
    }
    return;
}

int main() {
    memset(m, -1, sizeof m);
    for (int i = 0; i < 4; i++) cin >> m[i];
    bfs_find(0, 0);
    bfs_road(3, 5);
    for (int i = road.size(); i >= 0; i--) {
        printf("%c", road[i]);
    }
    cout << dist[3][5] << endl;
    return 0;
}