2025蓝桥杯十六届C++ B组题解与代码分析

填空题解析

A. 移动距离(5分)

cpp 复制代码
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    const int x = 233, y = 666;
    // 计算极坐标系半径
    const double r = hypot(x, y);
    // 计算旋转角度(弧度制)
    const double theta = atan2(y, x);
    // 总距离 = 直线移动 + 弧线移动
    const double total = r + r * theta;
    // 四舍五入取整输出
    cout << static_cast<int>(total + 0.5) << endl; // 输出1576
    return 0;
}

B. 客流量上限(5分)

cpp 复制代码
#include <iostream>
using namespace std;
const int MOD = 1e9+7;

int fast_pow(int base, int exp) {
    int res = 1;
    while (exp > 0) {
        if (exp & 1) res = 1LL * res * base % MOD;
        base = 1LL * base * base % MOD;
        exp >>= 1;
    }
    return res;
}

int main() {
    const int n = 2025;
    // 计算指数 (2025-1)/2 = 1012
    cout << fast_pow(2, (n-1)/2) << endl; // 输出781448427
    return 0;
}

编程题详解

C. 可分解的正整数(10分)

cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    int n, cnt = 0;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        // 数学结论:除1以外所有数都符合条件
        cnt += (x != 1);
    }
    cout << cnt;
    return 0;
}

D. 产值调整(10分)

cpp 复制代码
#include <iostream>
using namespace std;

void process(int& a, int& b, int& c, int k) {
    while (k--) {
        int na = (b + c) / 2;
        int nb = (a + c) / 2;
        int nc = (a + b) / 2;
        
        // 提前终止条件:数值不再变化
        if (na == a && nb == b && nc == c) break;
        
        a = na; b = nb; c = nc;
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int a, b, c, k;
        cin >> a >> b >> c >> k;
        process(a, b, c, k);
        cout << a << " " << b << " " << c << endl;
    }
    return 0;
}

E. 画展布置(15分)

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;
    vector<long long> nums(N);
    for (auto& num : nums) cin >> num;
    
    // 排序后滑动窗口找最小差值
    sort(nums.begin(), nums.end());
    
    long long min_val = 1e18;
    // 窗口滑动范围[i, i+M-1]
    for (int i = 0; i <= N - M; ++i) {
        long long diff = nums[i+M-1] * nums[i+M-1] - nums[i] * nums[i];
        min_val = min(min_val, diff);
    }
    
    cout << min_val;
    return 0;
}

F. 水质检测(15分)

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
    int n;
    string grid[2];
    cin >> n >> grid[0] >> grid[1];

    // dp[i][s]: 处理到第i列时状态s的最小操作数
    // s状态编码: 00 01 10 11 (二进制表示上下位置是否连通)
    vector<vector<int>> dp(n+1, vector<int>(4, INF));
    dp[0][3] = 0; // 初始状态:两行都需要连通

    for (int i = 1; i <= n; ++i) {
        // 当前列需要修改的格子数
        int cost0 = (grid[0][i-1] == '.') ? 1 : 0;
        int cost1 = (grid[1][i-1] == '.') ? 1 : 0;

        // 状态转移枚举
        for (int prev : {0,1,2,3}) {  // 前驱状态
            for (int curr : {0,1,2,3}) {  // 当前状态
                // 必须保证纵向和横向连通性
                if ((curr & prev) || (curr & (curr>>1))) {
                    int new_cost = dp[i-1][prev] + __builtin_popcount(curr) * (cost0 + cost1);
                    dp[i][curr] = min(dp[i][curr], new_cost);
                }
            }
        }
    }

    cout << *min_element(dp[n].begin(), dp[n].end());
    return 0;
}

相关推荐
倔强的小石头_2 小时前
【C语言指南】函数指针深度解析
java·c语言·算法
Yasin Chen2 小时前
C# Dictionary源码分析
算法·unity·哈希算法
_Coin_-3 小时前
算法训练营DAY27 第八章 贪心算法 part01
算法·贪心算法
董董灿是个攻城狮7 小时前
5分钟搞懂什么是窗口注意力?
算法
Dann Hiroaki7 小时前
笔记分享: 哈尔滨工业大学CS31002编译原理——02. 语法分析
笔记·算法
xiaolang_8616_wjl8 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
夜月yeyue8 小时前
设计模式分析
linux·c++·stm32·单片机·嵌入式硬件
qqxhb9 小时前
零基础数据结构与算法——第四章:基础算法-排序(上)
java·数据结构·算法·冒泡·插入·选择
无小道9 小时前
c++-引用(包括完美转发,移动构造,万能引用)
c语言·开发语言·汇编·c++
FirstFrost --sy11 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先