第十二届蓝桥杯省赛第二场C/C++大学B组编程题题目及解析

本文介绍了四个编程题目,涉及特殊日期判断、小平方数计算、完全平方数检测以及负载均衡和国际象棋的算法实现,展示了不同领域的信息技术挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

目录

 试题F:特殊年份

试题G:小平方 

 试题H:完全平方数

 试题I:负载平衡

试题J:国际象棋 


 试题F:特殊年份

在这里插入图片描述

#include <iostream>

using namespace std;

const int N = 5;

int res;

int a[N];

int main()
{
    for(int i = 0; i < 5; i ++)
    {
        int x;
        cin >> x;
        for(int j = 0; j < 5; j ++)
        {
            a[j] = x % 10; //a 0,1,2,3 分表表示个十百千位
            x /= 10;
        }
        if(a[1] == a[3] && a[0] - a[2] == 1)
            res ++;
    }
    cout << res;
    return 0;
}

试题G:小平方 

在这里插入图片描述

枚举 

#include <iostream>

using namespace std;

const int N = 10010;

int res;

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i < (int)n; i ++)
    {
        if((i * i) % n < (double)n / 2)
            res ++;
    }
    cout << res;
    return 0;
}

 试题H:完全平方数

在这里插入图片描述 

#include <iostream>

using namespace std;

typedef long long LL;

int main()
{
    LL n;
    LL res = 1;
    cin >> n;
    for(LL i = 2; i * i <= n; i ++)
    {
        LL s = 0;
        while(n % i == 0)
        {
            n /= i;
            s ++;
        }
        if(s % 2)  res *= i;
    }
    if(n > 1)   res *= n;
    cout << res;
    return 0;
}
    

 试题I:负载平衡

在这里插入图片描述

在这里插入图片描述 

 模拟 + 优先队列

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

typedef pair<int, int> PII;// <结束时间,任务算力>
const int N = 200010;

int n, m;
int s[N]; // 剩余算力
priority_queue<PII, vector<PII>, greater<PII>> q[N]; // <类型,存储方式,比较函数>,结构体小根堆比较结构体的第一个变量

int main()
{
    scanf("%d%d",&n, &m);
    for (int i = 1; i <= n; i ++) scanf("%d",&s[i]);
    while (m --)
    {
        int a, b, c, d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        while(q[b].size() && q[b].top().x <= a)
        {
            s[b] += q[b].top().y;
            q[b].pop();
        }
        if (s[b] < d) printf("-1\n");
        else
        {
            q[b].push({a + c, d});
            s[b] -= d;
            printf("%d\n",s[b]);
        }
    }
    return 0;
}

试题J:国际象棋 

在这里插入图片描述 

在这里插入图片描述 

状态压缩DP

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110, M = 1 << 6, K = 21, MOD = 1e9 + 7;

int n, m, k;
int f[N][M][M][K];

int get_count(int x)
{
    int res = 0;
    while (x)
    {
        res ++ ;
        x -= x & -x;
    }
    return res;
}

int main()
{
    cin >> n >> m >> k;
    f[0][0][0][0] = 1;

    int cnt = 0;
    for (int i = 1; i <= m; i ++ )
        for (int a = 0; a < 1 << n; a ++ )
            for (int b = 0; b < 1 << n; b ++ )
            {
                if (a & (b << 2) || b & (a << 2)) continue;
                for (int c = 0; c < 1 << n; c ++ )
                {
                    if (c & (b << 2) || b & (c << 2)) continue;
                    if (c & (a << 1) || a & (c << 1)) continue;
                    int t = get_count(c);
                    for (int u = t; u <= k; u ++, cnt ++ )
                        f[i][b][c][u] = (f[i][b][c][u] + f[i - 1][a][b][u - t]) % MOD;
                }
            }

    int res = 0;
    for (int i = 0; i < 1 << n; i ++ )
        for (int j = 0; j < 1 << n; j ++ )
            res = (res + f[m][i][j][k]) % MOD;

    cout << res << endl;
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值