第十届蓝桥杯C++B组题解

国赛题确实不简单哇hhh

前6题没有太大难度。第7题DP分类讨论实在是太多了,完全可以当压轴来出了呜呜呜,第8题运气好的话还是能做出来的hhh,这种题往往都不难都是找规律。第9题线段树的运用,但是很暴力很暴力,也算是一个积累叭。第10题直接暴力搜惹。希望今年国赛能做出来8道题就已经非常非常非常满足惹

A 平方序列

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

using namespace std;

int main()
{
    int x, y;
    for (int i = 2020; i; i ++ )
    {
        x = i;
        int t = 2 * x * x - 2019 * 2019;
        y = sqrt(t);
        if (y * y == t)
            break;
    }
    
    cout << x + y << endl;
    return 0;
}

B 质数拆分

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;
const int N = 2022;
typedef long long LL;
vector<int> res;
LL f[N];

bool is_prime(int x)  // 判定质数
{
    if (x < 2) return false;
    for (int i = 2; i <= x / i; i ++ )
        if (x % i == 0)
            return false;
    return true;
}

int main()
{
    for (int i = 2; i <= 2019; i ++ )
        if (is_prime(i))
            res.push_back(i);
    
    f[0] = 1;
    for (int i = 0; i < res.size(); i ++ )
    {
        for (int j = 2019; j >= res[i]; j -- )
            f[j] += f[j - res[i]];
    }
    cout << f[2019] << endl;
    return 0;
}

C 拼接

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

using namespace std;
const int N = 10;

bool st[N][N];
int n = 7, ans;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int x, int y)
{
    if (x == 0 || y == 7) 
    {
        ans ++ ;
        return;
    }
    
    for (int i = 0; i < 4; i ++ )
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a > n || b < 0 || b > n) continue;
        if (st[a][b] || a == b) continue;
        
        st[a][b] = st[b][a] = true;
        dfs(a, b);
        st[a][b] = st[b][a] = false;
    }
}

int main()
{
    for (int i = 0; i <= n; i ++ )
    {
        st[i][i] = true;
        dfs(i, i);
        st[i][i] = false;
    }
    
    cout << ans << endl;
    return 0;
}

D 求值

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

using namespace std;

int count(int x)
{
    int cnt = 1;
    for (int i = 2; i <= x / i; i ++ )
    {
        int s = 0;
        while (x % i == 0)
        {
            x /= i;
            s ++ ;
        }
        cnt *= 1 + s;
    }
    
    if (x > 1) cnt *= 2;
    return cnt;
}

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

E 路径计数

这道题官网答案有误,比赛时是按照206这个答案几分的

#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;

using namespace std;
const int N = 10;

LL ans;
bool st[N][N];
int n = 6;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

bool check(int x)
{
    return x > 1 && x <= 11;
}

void dfs(int x, int y, int step)
{
    if (x == 0 && y == 0 && check(step))
    {
        ans ++ ;
        return ;
    }
    
    st[x][y] = true;
    for (int i = 0; i < 4; i ++ )
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= n) continue;
        if (st[a][b]) continue;
        
        dfs(a, b, step + 1);
    }
    st[x][y] = false;
}

int main()
{
    dfs(0, 1, 0);
    cout << ans * 2 << endl;
    
    return 0;
}

F 最优包含

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

using namespace std;
const int N = 1010;

char a[N], b[N];
int f[N][N];

int main()
{
    scanf("%s%s", a + 1, b + 1);
    
    int n = strlen(a + 1), m = strlen(b + 1);
    memset(f, 0x3f, sizeof f);
    
    for (int i = 0; i <= n; i ++ )
        f[i][0] = 0;

    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= i; j ++ )
        {
            if (a[i] == b[j])
            {
                f[i][j] = f[i - 1][j - 1];
            }
            else f[i][j] = min(f[i - 1][j], f[i - 1][j - 1] + 1);
        }
    cout << f[n][m] << endl;
    return 0;
}

G 排列数

#include <iostream>
using namespace std;
#define mod 123456
#define N 505
int dp[N][N];
int main()
{
    int n, k;
    cin >> n >> k;
    dp[1][0] = 1;
    for(int i = 2; i <= n; i++){//第i个数前面有j个折点
        dp[i][0] = 2;
        for(int j = 0; j <= i-2; j++){
            dp[i][j] %= mod;
            dp[i+1][j] += dp[i][j] * (j+1);
            dp[i+1][j+1] += dp[i][j] * 2;
            dp[i+1][j+2] += dp[i][j] * (i-j-2);
        }
    }
    cout << dp[n][k-1] % mod << endl;
    return 0;
}

H 解谜游戏

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

using namespace std;

int main()
{
    string a, b, c;
    int T;
    scanf("%d", &T);
    
    while (T -- )
    {
        cin >> c >> b >> a;
        bool flag = true;
        for (int i = 0; i < 4; i ++ )
        {
            map<char, int> mp;
            
            mp[a[i]] ++ ;
            
            mp[b[i]] ++ ;
            mp[b[i + 4]] ++ ;
            
            mp[c[i]] ++ ;
            mp[c[i + 4]] ++ ;
            mp[c[i + 8]] ++ ;
            
            if (mp['Y'] != 1 || mp['R'] != 2 || mp['G'] != 3)
            {
                flag = false;
                break;
            }
        }
        
        if (flag) puts("YES");
        else puts("NO");
    }
    return 0;
}

I 第八大奇迹

线段树模板题,比较简单但是没有学过,学完后更

在这里插入代码片

H 燃烧权杖

不会,抱歉

在这里插入代码片
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shirandexiaowo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值