第十届蓝桥杯完整版题解 (详细讲解) (新鲜出炉)

简单总结

简单总结一下本场蓝桥杯吧…客观的来讲, 题目较去年相比难度略有下降, 但报名人数再创新高…由此来看, 获奖容易, 但如果想拿省一, 恐怕还是要多点细心…未必要学多少算法…因为根本就没怎么考算法…全tm都是坑
还是要吐槽一下蓝桥杯的赛制, 实时给个反馈不好么, 懒得花钱买服务器了? 很多题明明可以做对的, 真是醉了. 算了算大概100分左右吧…其实原本可以拿更高的分的…哎, 期待能给个省一吧.
不说了, 下面看题

考试原装PDF文件见文章末尾~

A: 组队 (签到题)

在这里插入图片描述

题解

能不写代码, 尽量不写代码, 直接挑出来最大的几个值, 反复取一下, 可以确定就是490了.
答案 : 490

B 年号子串(进制转换)

在这里插入图片描述

题解

很显然的进制转换题目, 用ABCD替换掉了1234, 注意26个字母应该是逢27进1, 也就是27进制, 而不是26进制

#include <cstdio>
#include <iostream>
using namespace std;
string solve(int n, int r){
    string ret;
    while(n > 0){
        int t = n%r;
        n = (n-1)/r;    //不是26进制
        if(t == 0) t = 26;
        ret += 'A'+t-1;
    }
    return ret;
}
int main()
{
    int n;
    cin >> n;
    string ans = solve(n, 26);
    for(int i = ans.length()-1; i >= 0; i--)
        cout << ans[i];

	return 0;
}

答案

BYQ

另外给大家附一个我写的n转r进制的模板, 大家可以存一下

	void solve(int n, int r)
	{
	    char ans[maxn], index = 0;
	    ms(ans, 0);
	    while(n > 0){
	        int t = n%r;
	        n /= r;
	        if(t > 9)
	            ans[index++] = (t-10+'A');
	        else
	            ans[index++] = (t+'0');
	    }
	}
//从后往前输出

C 数列求值(斐波那契数列)

在这里插入图片描述

题解

很显然的斐波那契数列变形了, 因为只考虑后四位, 所以我们%100000就可以啦, 用数组来存20190324个数有点大了会很慢, 我们直接用a, b, c, d四个数辗转着来存就好啦

答案

4659

#include <cstdio>
#include <iostream>
using namespace std;
const int mod = 1e5;

int main()
{
    int a = 1, b = 1, c = 1, d;
    for(int i = 4; i <= 20190324; i++){
        d = (a+b+c)%mod;
        c = b, b = a, a = d;
    }
    cout << d << endl;

	return 0;
}

D 数的分解 (枚举)

在这里插入图片描述
哎, 这道题目真的是我的血泪史呀…其实就是一道简单的暴力题, 我考前看了各种乱七八糟的总结, 尤其是计蒜客说的那个填空题一定会有dp, 如果点开不做也是可以的…我tm简单看了一下以为是数位dp就直接跳过了, 结果再也没时间回来看, 10分啊, 苍天, 现在再看一下…卧槽, 只有三个数啊, 划重点.

题解

3层for循环, 定义一下枚举顺序, 控制一下不要出现重复的情况即可…

答案

40785

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

bool judge(int n){
    while(n>0){
        int t = n%10;
        n /= 10;
        if(t == 2 || t == 4)
            return false;
    }
    return true;
}
int main()
{
    int n, ans = 0;
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n && (n-i-j)>j; j++){
            if(judge(i) && judge(j) && judge(n-i-j))
                ans++;
        }
    }
    cout << ans << endl;
	return 0;
}

E 迷宫 (BFS+剪枝)

【问题描述】
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫,
一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,
其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务 必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt, 内容与下面的文本相同)

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

【答案提交】 这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一
个字符串,包含四种字母 D、U、L、R,在提交答案时只填写这个字符串,填 写多余的内容将无法得分。

题解

非常经典的迷宫问题了, 注意题目要求了求最短路和路径还原, 所以略微有点难度, 但做的方法还是非常多的, 由于数据量略大, 这里提醒必须使用最优化剪枝, 也就是用一个数组例如d[i][j]保存走到(i ,j)时的最短路径, 如果当前要走的超过了这个就不再走了
至于路径还原, 个人习惯用构造队列+BFS, 当然倒着搜也是可以的.

答案

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 1e6+10;
const int mod = 1e5;

int N, M, minD[55][55], minM = inf;     //保存走到(x,y)处的最短步数
int act[4][2] = {{1,0},{0,-1},{0,1},{-1,0}};
char maze[55][55], op[4] = {'D','L','R','U'};
string ans;
struct node{
    int x, y, cnt, last;    //last为上一步坐标, cnt为当前总步数
    char o;                 //当前操作字符
    node(int xx, int yy, int cc, int ll, char oo){x=xx, y=yy, cnt=cc, last=ll, o=oo;}
    node(){}
}q[55*55];
int head = 0, tail = 0;     //模拟队列
void Bfs(){
    for(int i = 0; i <= 30; i++)
        for(int j = 0; j <= 50; j++)
            minD[i][j] = inf;
    minD[1][1] = 0;

    q[tail++] = node(1,1,0,-1,' ');
    while(head < tail){
        node cur = q[head++];
        //printf("%d %d: %d\n",cur.x,cur.y,cur.cnt);
        //到达终点
        if(cur.x==N && cur.y==M){
            if(cur.cnt < minM){
                minM = cur.cnt;
                ans = "";
                while(cur.last!=-1){
                    ans += cur.o;
                    cur = q[cur.last];
                }
            }
            continue;
        }
        //继续搜索
        for(int i = 0; i < 4; i++){
            int cx = cur.x+act[i][0], cy = cur.y+act[i][1];
            if(cx>0&&cx<=N&&cy>0&&cy<=M && maze[cx][cy]=='0' && cur.cnt+1<minD[cx][cy]) //最优化剪枝
                q[tail++] = node(cx, cy, cur.cnt+1, head-1, op[i]), minD[cx][cy] = cur.cnt+1;
        }
    }
}
int main()
{
    cin >> N >> M;
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
            cin >> maze[i][j];
    Bfs();
    for(int i = ans.length()-1; i >= 0; i--)
        cout << ans[i];

	return 0;
}

F 特别数的和 (枚举 数位读取)

在这里插入图片描述

题解

这题还是比较简单的, 直接枚举n, 读取每一位判断就行

#include <bits/stdc++.h>
using namespace std;
int N;
bool isOK(int x) {
    for (/* */; x > 0; x /= 10) {
        int t = x % 10;
        if (t % 10 == 2 || t % 10 == 0 || t % 10 == 1 || t % 10 == 9)
            return true;
    }
    return false;
}
int main() {
    cin >> N;
    int ret = 0;
    for (int i = 1; i <= N; ++i)
        if (isOK(i)) ret += i;
    cout << ret << endl;
    return 0;
}

G完全二叉树的值 (二叉树遍历)

在这里插入图片描述

题解

完全二叉树就是满二叉树啦, 没有学过的同学也不要紧, 仔细观察一下可以发现一次都是2的倍数, 每次遍历2^i就好啦, 切记题目上指定的A的值可能为负数啊啊啊啊啊啊, 要初始化为-inf

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
int N, a[MAXN + 1];
int main() {
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i)
        scanf("%d", a + i);
    long long m = -MAXN;
    int ans = 0;
    for (int i = 1; (1 << (i - 1)) < N; ++i) {
        long long t = 0;
        for (int j = 1 << (i - 1); j < (1 << i) && j < N; ++j)
            t += a[j];
        if (m < t) {
            m = t;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}

H 等差数列(gcd)

在这里插入图片描述

题解

先排序, 然后取出这些数差的GCD即为公差, 注意特判公差为0的情况
比赛时候怎么没想起来就是GCD啊呜呜呜呜

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 1e5+10;


int n, a[maxn];
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a+1, a+1+n);

    int d = a[2]-a[1];
    if(d==0)
        cout << "0\n";
    else{
        for(int i = 3; i <= n; i++)
            d = __gcd(d, a[i]-a[i-1]);
        cout << (a[n]-a[1])/d+1 << endl;;
    }

	return 0;
}

I 后缀表达式(贪心)

在这里插入图片描述

题解

划重点! 后缀表达式啊啊啊, 之前的每周一题里面有一道几乎是原题的…
其实所谓的后缀表达式, 就可以理解成可以添加任意数量的括号就可以了
例如对于N=2, M=1, -1 -2 -3 -4
最优解应该是-1 - (-2 + -3 + -4)
总结一下, 就是

  1. 如果有减号的话, 结果为最小数的相反数加上其他所有数绝对值之和
  2. 否则, 所有数的和为答案
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
int a[MAXN + MAXM + 1], N, M;
int main() {
    scanf("%d%d", &N, &M);
    int mini = INF;
    for (int i = 0; i <= N + M; ++i) {
        scanf("%d", a + i);
        mini = min(a[i], mini);
    }
    long long ret = 0;
    if (M) {
        for (int i = 0; i <= N + M; ++i)
            ret += abs(a[i]);
        if (mini > 0) ret -= mini * 2;
    }
    else for (int i = 0; i <= N + M; ++i)
        ret += a[i];
    cout << ret << endl;
    return 0;
}

J 灵能传输

题解

同求

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/88791546