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

发布于:2025-05-09 ⋅ 阅读:(18) ⋅ 点赞:(0)

填空题解析

A. 移动距离(5分)

#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分)

#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分)

#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分)

#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分)

#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分)

#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;
}


网站公告

今日签到

点亮在社区的每一天
去签到