2021届蓝桥杯B组详细题解

有的时候根本不用写代码来计算选择题,可以直接选择计算器或者是Excel;
比如这次得A题我是用Excel写的,hhh

A题 空间
A题注意:8bit = 1B
256 * 8 * (2^20) / 32 = 67108864 
答案:67108864
B题 卡片
//暴力可解 时间复杂度不超过10^7- 10^8就ok
#include <algorithm>
#include <iostream>

using namespace std;

int a[10];

int main()
{
    int i, y = 0;
    for (i = 1;i <= 100000; i ++)
    {
        int b = i;
        while (b > 0)
        {
            a[b % 10]++;
            if (a[b % 10] > 2021)
            {
                y = 1;
                break;
            }
            b /= 10;
        }
        if (y == 1) break;
    }
    cout << i - 1 << endl;
    return 0;
}
C题 直线
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 200000;

int n;
struct Line{
double k, b;
}l[N];
bool cmp(Line x, Line y)
{
    if (x.k != y.k) return x.k < y.k;
    return x.b < y.b;
}

int main()
{
    for (int x1 = 0; x1 < 20; x1 ++)
    {
        for (int y1 = 0; y1 < 21; y1 ++)
        {
            for (int x2 = 0; x2 < 20; x2 ++)
            {
                for (int y2 = 0; y2 < 21; y2 ++)
                {
                    if (x1 != x2)
                    {
                        double k = (double) (y1 - y2) / (x1 - x2);
                        double b = y1 - k * x1;
                        l[n ++] = {k, b};
                    }
                }
            }
        }
    }
    sort (l , l + n);
    int res = 1;
    for (int i = 1; i < n; i ++)
    {
        if (fabs(l[i].k - l[i - 1].k) > 1e-8 || fabs(l[i].b - l[i - 1].b) > 1e-8)//double有精度问题 如果直接==结果会小
            res++;
        cout << res + 20 << endl;//+20条竖线
        return 0;
    }
}
D题 货物摆放
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long LL;

int main()
{
    LL n;
    cin >> n;
    vector <LL> d;
    for (LL i = 1; i * i <= n; i ++ )
        if (n % i == 0)
        {
            d.push_back(i);
            if (n / i != i) d.push_back(n / i);//求约数
        }

    int res = 0;
    for (auto a: d)//暴力枚举
        for (auto b: d)
            for (auto c: d)
                if (a * b * c == n)
                    res ++ ;
    cout << res << endl;

    return 0;
}
E题 路径
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 2200, M = N * 50;

int n;
int h[N], e[M], w[M], ne[M], idx;//邻接表
int q[N], dist[N];//存最短路
bool st[N];

int gcd(int a, int b)  // 欧几里得算法 求最大公约数
{
    return b ? gcd(b, a % b) : a;
}

void add(int a, int b, int c)  // 添加一条边a->b,边权为c
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void spfa()  // 求1号点到n号点的最短路距离
{
    int hh = 0, tt = 0;
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    q[tt ++ ] = 1;
    st[1] = true;

    while (hh != tt)
    {
        int t = q[hh ++ ];
        if (hh == N) hh = 0;
        st[t] = false;

        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                if (!st[j])     // 如果队列中已存在j,则不需要将j重复插入
                {
                    q[tt ++ ] = j;
                    if (tt == N) tt = 0;
                    st[j] = true;
                }
            }
        }
    }
}


int main()
{
    n = 2021;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= n; i ++ )
        for (int j = max(1, i - 21); j <= min(n, i + 21); j ++ )
        {
            int d = gcd(i, j);
            add(i, j, i * j / d);//i * j / b 是最小公倍数
        }

    spfa();
    printf("%d\n", dist[n]);
    return 0;
}
F 时间显示
//此题注意  坑死了 我居然认为1s = 60ms 傻死了 浪费了我一个小时 
//注意1s = 1000ms
#include <algorithm>
#include <iostream>
#include <cstdio>

using namespace std;

long long a,b,c,h,m,s;

int main()
{
    cin >> a;
    b = a / 1000;
    s = b % 60;
    c = b / 60;
    m = c % 60;
    h = c / 60 % 24;
    if (h < 10) cout << 0 << h << ":";
    else cout << h << ":";
    if (m < 10) cout << 0 << m << ":";
    else cout << m << ":";
    if (s < 10) cout << 0 << s << ":";
    else cout << s;
    return 0;
}
G题 砝码称重
//背包问题
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 110, M = 200010, B = M / 2;
int n,m;
int w[N];
int f[N][M];

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n ; i++) scanf("%d",&w[i]);
    f[0][B] = true;
    for (int i = 1;i <= n; i ++)
    {
        for (int j = -m; j <= m; j++)
        {
            f[i][j + B] = f[i - 1][j + B];
            if (j - w[i] >= -m) f[i][j + B] |= f[i - 1][j - w[i] + B];
            if (j + w[i] <= m) f[i][j + B] |= f[i - 1][j + w[i] + B];
        }
    }
    int res = 0;
    for (int j = 1; j <= m; j ++)
    {
        if (f[n][j + B]) res++;
    }
    printf("%d\n",res);
    return 0;
}
H题 杨辉三角
//暴力可解
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

const int N = 110;
unsigned long long f[N][N];
unsigned long long n;

int main()
{
    cin >> n;
    f[1][1] = 1;
    for (int i = 2; i <= 100; i ++)
    {
        for (int j = 1;j <= i; j ++)
        {
            if (j == 1 || j == i) f[i][j] = 1;
            else f[i][j] = f[i - 1][j - 1] + f[i - 1][j];
        }
    }
    int k = 0, y = 0;
    for (int i = 1; i <= 100; i ++)
    {
        for (int j = 1; j <= i; j ++)
        {
            k ++;
            if (f[i][j] == n)
            {
                y = 1;
                break;
            }
        }
        if (y == 1) break;
    }
    cout << k << endl;
    return 0;
}
I题 双向排序
//直接sort简单 不超时
#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1e5 + 10;
int n,m;
int p,q;
int a[N];

bool cmp(int x, int y)
{
    return x > y;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++) a[i] = i + 1;
    while (m --)
    {
        cin >> p >> q;
        if (p == 0)
        {
            sort(a, a + q, cmp);
        }
        else
        {
            sort(a + q - 1, a + n);
        }
    }
    for (int i = 0; i < n; i ++) cout << a[i] << " ";
    cout << endl;
    return 0;
}
J题 括号序列

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值