【蓝桥杯】第十五届蓝桥杯C/C++B组省赛补题

2024-05-01 12:12

本文主要是介绍【蓝桥杯】第十五届蓝桥杯C/C++B组省赛补题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 估分
    • 试题 A: 握手问题
    • 试题 B: 小球反弹
    • 试题 C: 好数
    • 试题 D: R 格式
    • 试题 E: 宝石组合
    • 试题 F: 数字接龙
    • 试题 G: 爬山
    • 试题 H: 拔河

估分

测试网址:民间测试数据

5 + 0 + 9 + 5 + 2 + 5 + 18 + 2 = 46 5 + 0 + 9 + 5 + 2 + 5 + 18 + 2 =46 5+0+9+5+2+5+18+2=46

试题 A: 握手问题

#include <bits/stdc++.h>using namespace std;typedef pair<int, int> PII;void solve() {set<PII> s;int res = 7 * 43;for (int i = 1; i <= 43; i++)for (int j = 1; j <= 43; j++)if (i != j)s.insert({min(i, j), max(i, j)});	cout << res + s.size() << endl;//ans = 1204
} int main() {int t = 1;//cin >> t;while (t --) solve();return 0;
}

试题 B: 小球反弹

不会

试题 C: 好数

这题数据 1 e 7 1e7 1e7,暴力跑民间数据(92分,估分9分)

#include <bits/stdc++.h>using namespace std;bool check(int x) {vector<int> v;while (x) {v.push_back(x % 10);x /= 10;}for (int i = 0; i < v.size(); i++) {if (i % 2 == 0 && v[i] % 2 == 0) //奇数位 是偶数 return false;if (i % 2 != 0 && v[i] % 2 != 0) // 偶数位 是奇数 return false;}return true;
}void solve() {int n;cin >> n;int res = 0;for (int i = 1; i <= n; i++) {if (check(i)) res ++;}cout << res << endl;
}signed main() {int t = 1;//cin >> t;while (t --) solve();return 0;
}

试题 D: R 格式

考察高精度乘法、高精度加法,考场上直接暴力写的(民间测试52分,估分5分)

#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 1e5 + 7, inf = 0x3f3f3f3f, mod = 1e9 + 7;
// 浮点数 高精度乘法 ? 
void solve() {long double n, d;cin >> n >> d;long double res = 1.0 * d * (pow(2, n));//cout << fixed << setprecision(0) << (long double)(pow(2, n)) << endl;cout << fixed << setprecision(0) << res << endl;
}signed main() {int t = 1;//cin >> t;while (t --) solve();return 0;
}

正解:模拟高精度乘法,高精度加法

#include <bits/stdc++.h>
using namespace std;int n; string d;
vector<int> A, B;vector<int> mul(vector<int> &A, int b) {vector<int> C;int t = 0;for (int i = 0; i < A.size() || t; i++) {if (i < A.size()) t += A[i] * b;C.push_back(t % 10);t /= 10;}while (C.size() > 1 && C.back() == 0) C.pop_back();return C;
}vector<int> mul(vector<int> &A, vector<int> &B) {vector<int> C(A.size() + B.size() + 7, 0);for (int i = 0; i < A.size(); i++) {for (int j = 0; j < B.size(); j++) {C[i + j] += A[i] * B[j];}}for (int i = 0; i + 1 < C.size(); i++) {C[i + 1] += C[i] / 10;C[i] %= 10;}while (C.size() > 1 && C.back() == 0) C.pop_back();reverse(C.begin(), C.end());return C;
}vector<int> add(vector<int> &A, vector<int> &B) {if (A.size() < B.size()) return add(B, A);vector<int> C;int t = 0;for (int i = 0; i < A.size(); i++) {t += A[i];if (i < B.size()) t += B[i];C.push_back(t % 10);t /= 10;}if (t) C.push_back(t);return C;
}int main() {cin >> n >> d;A.push_back(1);while (n--) A = mul(A, 2);//for (auto c : A) cout << c; cout << endl;int len = d.size() - d.find('.') - 1;reverse(d.begin(), d.end());for (auto c: d) {if (c != '.')B.push_back(c - '0');}//for (auto c : B) cout << c; cout << endl;auto res = mul(A, B);//for (auto c : res) cout << c; cout << endl;int last = -1;while (len--) last = res.back(), res.pop_back();if (last >= 5) {vector<int> base;base.push_back(1);reverse(res.begin(), res.end());res = add(res, base);reverse(res.begin(), res.end());}for (auto c: res) cout << c;
}

试题 E: 宝石组合

数论,赛场暴力写的民间测试过了一个点 ,估分2分

试题 F: 数字接龙

赛场调dfs调了很久调不出来,最后输出-1民间测试得分一半, 估分5分吧。正解dfs搜索,注意数学中的直角坐标方向与二维数组中的方向是不同的,按0,1,2…8的方向进行搜索,即可得到字典序最小的答案。最重要的问题是判路径是否交叉,可以拿一个dir数组记录当前遍历位置所走的方向(0~8),判断交叉共有四种情况即方向为1,3,5,7时,四种情况如下:
i == 1 && (dir[x - 1][y] == 3 || dir[x][y + 1] == 7)
i == 3 && (dir[x + 1][y] == 1 || dir[x][y + 1] == 5)
i == 5 && (dir[x][y - 1] == 3 || dir[x + 1][y] == 7)
i == 7 && (dir[x][y - 1] == 1 || dir[x - 1][y] == 5)
参考: 数字接龙-蓝桥杯

#include <bits/stdc++.h>using namespace std;typedef pair<int ,int> PII;
const int N = 10 + 7;
// int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; // 按照 0 1 2 3 ... 8的方向遍历
int n, k, g[N][N], dir[N][N], st[N][N];
vector<int> ans;bool dfs(int x, int y, int cnt) {if (x == n - 1 && y == n - 1 && cnt == n * n) return true;for (int i = 0; i < 8; i++) {int a = x + dx[i], b = y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n || st[a][b]) continue;if ((g[a][b] != g[x][y] + 1 && g[x][y] >= 0 && g[x][y] < k - 1) || (g[a][b] != 0 && g[x][y] == k - 1)) continue;//检查是否路径交叉 if (i == 1 && (dir[x - 1][y] == 3 || dir[x][y + 1] == 7)) continue;if (i == 3 && (dir[x + 1][y] == 1 || dir[x][y + 1] == 5)) continue; if (i == 5 && (dir[x][y - 1] == 3 || dir[x + 1][y] == 7)) continue; if (i == 7 && (dir[x][y - 1] == 1 || dir[x - 1][y] == 5)) continue; st[x][y] = 1, dir[x][y] = i;ans.push_back(i);if (dfs(a, b, cnt + 1)) return true;ans.pop_back();st[x][y] = 0, dir[x][y] = -1; //回溯}return false;
}void solve() {cin >> n >> k;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)cin >> g[i][j];if (g[n - 1][n - 1] != k - 1 || g[0][0] != 0) {cout << -1; return ;}	memset(dir, -1, sizeof dir);st[0][0] = 1;if (dfs(0, 0, 1)) {for (auto c : ans) cout << c;} else {cout << -1;}
}signed main() {int t = 1;//cin >> t;while (t --) solve();return 0;
}

试题 G: 爬山

思路:贪心 + 堆 , 每次用使用消除更多的方法,民间数据(92分,估分18分)

#include <bits/stdc++.h>#define x first
#define y second
#define int long longusing namespace std;typedef pair<int ,int> PII;
const int N = 1e5 + 7, inf = 0x3f3f3f3f, mod = 1e9 + 7;
int n, k, q, p, x;//贪心 堆 void solve() {cin >> n >> p >> q;priority_queue<int, vector<int>> heap;for (int i = 1; i <= n; i++) {cin >> x;heap.push(x);}while (!heap.empty()) {auto t = heap.top();if (p <= 0 && q <= 0) break;if (p > 0 && q > 0) {heap.pop();int det1 = abs(t - floor(sqrt(t))), det2 = t / 2;if (det1 >= det2) heap.push(floor(sqrt(t))), p --;elseheap.push(t / 2), q --;}else if (p > 0 && q <= 0) {heap.pop();heap.push(floor(sqrt(t))), p --;} else if (p <= 0 && q > 0) {heap.pop();heap.push(t / 2), q --;} }int res = 0;while (!heap.empty()) {res += heap.top();//cout << heap.top() << endl;heap.pop();}cout << res << endl;
}signed main() {int t = 1;//cin >> t;while (t --) solve();return 0;
}

试题 H: 拔河

没读懂题,前缀和枚举思路,民间测试10分,估分2分。
题意是任意选出两队,不要求选完,考场上误以为要选完,直接枚举中点前缀和处理了。

这篇关于【蓝桥杯】第十五届蓝桥杯C/C++B组省赛补题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/951655

相关文章

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ