欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 2025蓝桥杯十六届C++ B组题解与代码分析

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

2025/5/9 9:12:35 来源:https://blog.csdn.net/weixin_53920044/article/details/147674027  浏览:    关键词:2025蓝桥杯十六届C++ B组题解与代码分析

填空题解析

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; // 输出1576return 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 = 1012cout << fast_pow(2, (n-1)/2) << endl; // 输出781448427return 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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词