【蓝桥杯】考前冲刺!

发布于:2025-04-05 ⋅ 阅读:(19) ⋅ 点赞:(0)

在这里插入图片描述

个人主页:Guiat
归属专栏:算法竞赛

在这里插入图片描述

正文

总共6道真题,围绕蓝桥杯高频考点逐一展开。

1. 暴力枚举 — 好数

【题目】好数

【AC_Code】

#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int N, digit, cnt;

bool check(int n)
{
    digit = 1;
    while (n)
    {
        if (digit % 2 == 1) { if ((n % 10) % 2 == 0) return false; }
        else { if ((n % 10) % 2 != 0) return false; }
        digit ++; n /= 10;
    }
    return true;
}

void solve()
{
    cin >> N;
    for (int i = 1; i <= N; i ++) if (check(i)) cnt ++;
    cout << cnt << '\n';
}

int main()
{
    IOS; int _ = 1; // cin >> _;
    while (_ --) solve();
    
    return 0;
}

2. 打表规律 — 数字诗意

【题目】数字诗意

【AC_Code】

#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;

int n, cnt; ll a;

void solve()
{
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
        cin >> a;
        for (int j = 0; j <= 54; j ++)
        {
            if (a == pow(2, j)) cnt ++;
        }
    }
    cout << cnt << '\n';
}

int main()
{
    IOS; int _ = 1; // cin >> _;
    while (_ --) solve();
    
    return 0;
}

3. 数论入门 — 宝石组合

【题目】宝石组合

【AC_Code】

#include <iostream>
#include <vector>
#include <algorithm>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int MAXN = 1e5 + 10; int n, h[MAXN]; vector<int> fac[MAXN];

void solve()
{
    cin >> n; for (int i = 1; i <= n; ++ i) cin >> h[i];
    sort(h + 1, h + 1 + n);
    for (int i = 1; i <= n; ++ i) for (int j = 1; j * j <= h[i]; ++j)
	{
        if (h[i] % j == 0)
		{
            fac[j].push_back(h[i]);
            if (h[i] / j != j) fac[h[i] / j].push_back(h[i]);
        }
    }

    for (int i = MAXN; i >= 1; -- i)
	{
        if (fac[i].size() >= 3)
		{
            int a = fac[i][0], b = fac[i][1], c = fac[i][2];
            if (__gcd(__gcd(a, b), c) == i) { cout << a << " " << b << " " << c << "\n"; return; }
        }
    }
}

int main()
{
    IOS; solve();

    return 0;
}

4. 排序策略 — 封闭图形个数

【题目】封闭图形个数

【AC_Code】

#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int n, a, num, cnt[10] = { 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 }, t;
vector<pair<int, int>> arr;

void solve()
{
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
        cin >> a; t = a; num = 0;
        while (t) { num += cnt[t % 10]; t /= 10; }
        arr.push_back({ num, a });
    }
    sort(arr.begin(), arr.end());
    for (int i = 0; i < n; i ++) cout << arr[i].second << " \n"[i == n - 1];
}

int main()
{
    IOS; int _ = 1; // cin >> _;
    while (_ --) solve();
    
    return 0;
}

5. 贪心策略 — 训练士兵

【题目】训练士兵

【AC_Code】

#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;

const int N = 1e6 + 10;
ll n, S, p, c, ans; map<ll, ll> mp;

void solve()
{
    cin >> n >> S;
    for (int i = 1; i <= n; i ++) { cin >> p >> c; mp[c] += p; }
    for (int i = N; i >= 1; i --) mp[i] += mp[i + 1];    // 后缀和 
    for (int i = 1; i <= N; i ++) ans += min(S, mp[i]);
    cout << ans << '\n';
}

int main()
{
    IOS; int _ = 1; // cin >> _;
    while (_ --) solve();
    
    return 0;
}

6. 哈希技巧 — 团建

【题目】团建

【AC_Code】

#include <bits/stdc++.h>
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 2e5 + 10;
int n, m, c[N], d[N], u, v, ans;
map<int, vector<int>> m1, m2;

void dfs(int x, int y, int cnt)
{
    if (c[x] != d[y]) return ;
    ans = max(ans, cnt + 1);
    for (int i = 0; i < m1[x].size(); i ++)
        for (int j = 0; j < m2[y].size(); j ++)
            dfs(m1[x][i], m2[y][j], cnt + 1);
}

void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) cin >> c[i];
    for (int i = 1; i <= m; i ++) cin >> d[i];
    for (int i = 1; i <= n - 1; i ++) cin >> u >> v, m1[u].push_back(v);
    for (int i = 1; i <= m - 1; i ++) cin >> u >> v, m2[u].push_back(v);
    dfs(1, 1, 0); cout << ans << '\n';
}

int main()
{
    IOS; int _ = 1; // cin >> _;
    while (_ --) solve();
    
    return 0;
}

结语
感谢您的阅读!期待您的一键三连!欢迎指正!

在这里插入图片描述