【蓝桥杯】第十五届蓝桥杯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

相关文章

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

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

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

C++/类与对象/默认成员函数@构造函数的用法

《C++/类与对象/默认成员函数@构造函数的用法》:本文主要介绍C++/类与对象/默认成员函数@构造函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录名词概念默认成员函数构造函数概念函数特征显示构造函数隐式构造函数总结名词概念默认构造函数:不用传参就可以

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

C/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元