【蓝桥杯】第十五届蓝桥杯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++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么