2024年第十五届蓝桥杯C++B组个人解 您所在的位置:网站首页 宝藏颜色 2024年第十五届蓝桥杯C++B组个人解

2024年第十五届蓝桥杯C++B组个人解

2024-07-11 07:57| 来源: 网络整理| 查看: 265

2024年第十五届蓝桥杯C++B组 A: 握手问题(5分)问题描述思路 B: 小球反弹(5分)问题描述思路 C: 好数(10分)问题描述输入格式输出格式样例输入1样例输出1样例输入2样例输出2样例说明评测用例规模与约定思路 D: R 格式(10分)问题描述输入格式输出格式样例输入样例输出样例说明评测用例规模与约定思路 E: 宝石组合(15分)问题描述输入格式输出格式样例输入样例输出评测用例规模与约定思路 F: 数字接龙(15分)问题描述输入格式输出格式样例输入样例输出样例说明评测用例规模与约定思路 G: 爬山(20分)问题描述输入格式输出格式样例输入样例输出样例说明评测用例规模与约定思路 H: 拔河(20分)问题描述输入格式输出格式样例输入样例输出样例说明评测用例规模与约定思路 博客地址:https://blog.csdn.net/m0_46326495/article/details/137728637

A: 握手问题(5分) 问题描述

小蓝组织了一场算法交流会议,总共有 50 50 50 人参加了本次会议。在会议上,大家进行了握手交流。按照惯例他们每个人都要与除自己以外的其他所有人进行一次握手(且仅有一次)。但有 7 7 7 个人,这7 人彼此之间没有进行握手(但这 7 7 7 人与除这 7 7 7 人以外的所有人进行了握手)。请问这些人之间一共进行了多少次握手? 注意 A A A 和 B B B 握手的同时也意味着 B B B 和 A A A 握手了,所以算作是一次握手。

思路

组合数学口算题, C 50 2 − C 7 2 = 1204 C_{50}^2-C_{7}^2=1204 C502​−C72​=1204

B: 小球反弹(5分) 问题描述

有一长方形,长为 343720 343720 343720 单位长度,宽为 233333 233333 233333 单位长度。在其内部左上角顶点有一小球(无视其体积),其初速度如图所示且保持运动速率不变,分解到长宽两个方向上的速率之比为 d x : d y = 15 : 17 dx : dy = 15 : 17 dx:dy=15:17。小球碰到长方形的边框时会发生反弹,每次反弹的入射角与反射角相等,因此小球会改变方向且保持速率不变(如果小球刚好射向角落,则按入射方向原路返回)。从小球出发到其第一次回到左上角顶点这段时间里,小球运动的路程为多少单位长度?答案四舍五入保留两位小数。

在这里插入图片描述

思路

也是数学题,最终返回左上角时,走过的水平路程和垂直路程一定是 343720 343720 343720和 233333 233333 233333的偶数倍,并且水平路程与垂直路程之比一定为 15 : 17 15:17 15:17。写暴力去找结果即可,答案是 1100325199.77 1100325199.77 1100325199.77

代码:

#include #include using namespace std; typedef long long ll; const ll W = 233333; const ll L = 343720; int main() { for (ll x = 2; x if (15 * W * y == 17 * L * x) { printf("%llf", sqrt((L * x) * (L * x) + (W * y) * (W * y))); return 0; } } } return 0; } C: 好数(10分) 问题描述

一个整数如果按从低位到高位的顺序,奇数位(个位、百位、万位· · · )上的数字是奇数,偶数位(十位、千位、十万位· · · )上的数字是偶数,我们就称之为“好数”。给定一个正整数 N N N,请计算从 1 1 1 到 N N N 一共有多少个好数。

输入格式

一个整数 N N N

输出格式

一个整数代表答案

样例输入1

24

样例输出1

7

样例输入2

2024

样例输出2

150

样例说明

对于第一个样例, 24 24 24 以内的好数有 1 1 1、 3 3 3、 5 5 5、 7 7 7、 9 9 9、 21 21 21、 23 23 23,一共 7 7 7 个。

评测用例规模与约定

对于 10 % 10\% 10% 的评测用例, 1 ≤ N ≤ 1 0 2 1 ≤ N ≤ 10^2 1≤N≤102 对于 100 % 100\% 100% 的评测用例, 1 ≤ N ≤ 1 0 7 1 ≤ N ≤ 10^7 1≤N≤107

思路

正解应该是数位dp,但是感觉数据不大,暴力枚举也能过

#include #include using namespace std; int n; bool inline judge(int x) { int k = 1; while (x) { if ((x & 1) != (k & 1)) { return false; } x /= 10; k++; } return true; } int main() { ios::sync_with_stdio(false); cout.tie(nullptr); cin >> n; int res = 0; for (int i = 1; i int sum = carry; if (i >= 0) sum += a[i--] - '0'; if (j >= 0) sum += b[j--] - '0'; carry = sum / 10; res += to_string(sum % 10); } if (carry) res += to_string(carry); reverse(res.begin(), res.end()); return res; } string mul(string a, string b) { string res = "0"; int n = a.size(); int m = b.size(); for (int i = n - 1; i >= 0; i--) { int carry = 0; string temp; for (int j = m - 1; j >= 0; j--) { int sum = (a[i] - '0') * (b[j] - '0') + carry; carry = sum / 10; temp += to_string(sum % 10); } if (carry) temp += to_string(carry); reverse(temp.begin(), temp.end()); for (int k = 0; k ios::sync_with_stdio(false); cout.tie(nullptr); cin >> n; int max_a = 0; for (int i = 1; i for (int j = i; j if (vec[i].size() >= 3) break; vec[i].push_back(j); } } } for (int i = max_a; i >= 2; i--) { if (vec[i].size() >= 3) { cout 0, 1, 1, 1, 0, -1, -1, -1}; int n, k; bool st[101][101], ss[11][11][11][11]; int a[101][101]; vector s; void dfs(int x, int y, string q) { if (x == n - 1 && y == n - 1) { if (q.size() == n * n - 1) { s.push_back(q); } return; } for (int i = 0; i st[ax][ay] = true; char qq = i + '0'; dfs(ax, ay, q + qq); st[ax][ay] = false; } else if (!st[ax][ay] && i % 2 == 1 && !ss[x][y][ax][ay]) { st[ax][ay] = true; char qq = i + '0'; if (i == 1) { ss[x - 1][y][ax + 1][ay] = true; ss[ax + 1][ay][x - 1][y] = true; } if (i == 3 || i == 5) { ss[x + 1][y][ax - 1][ay] = true; ss[ax - 1][ay][x + 1][y] = true; } dfs(ax, ay, q + qq); st[ax][ay] = false; if (i == 1) { ss[x - 1][y][ax + 1][ay] = false; ss[ax + 1][ay][x - 1][y] = false; } if (i == 3 || i == 5) { ss[x + 1][y][ax - 1][ay] = false; ss[ax - 1][ay][x + 1][y] = false; } } } } int main() { ios::sync_with_stdio(false); cout.tie(nullptr); cin >> n >> k; for (int i = 0; i cin >> a[i][j]; } } string q; st[0][0] = true; dfs(0, 0, q); if (s.empty()) { cout ios::sync_with_stdio(false); cout.tie(nullptr); cin >> n >> p >> q; for (int i = 0; i int cur = pq.top(); pq.pop(); if ((int) sqrt(cur) q--; cur = (int) (cur / 2); } pq.push(cur); } while (p != 0) { int cur = pq.top(); pq.pop(); p--; cur = (int) sqrt(cur); pq.push(cur); } while (q != 0) { int cur = pq.top(); pq.pop(); q--; cur = (int) (cur / 2); pq.push(cur); } ll ans = 0; while (!pq.empty()) { ans += pq.top(); pq.pop(); } cout al1​​,al1​+1​,...,ar1​−1​,ar1​​}和 { a l 2 , a l 2 + 1 , . . . , a r 2 − 1 , a r 2 } \{a_{l_2} , a_{{l_2}+1},...,a_{{r_2}-1},a_{{r_2}}\} {al2​​,al2​+1​,...,ar2​−1​,ar2​​},其中 l 1 ≤ r 1 < l 2 ≤ r 2 l_1 ≤ r_1 < l_2 ≤ r_2 l1​≤r1​a4​;a5​},力量值和分别为 10 + 9 + 8 = 27 10 + 9 + 8 = 27 10+9+8=27, 12 + 14 = 26 12 + 14 = 26 12+14=26,差距为 ∣ 27 − 26 ∣ = 1 |27 − 26| = 1 ∣27−26∣=1。

评测用例规模与约定

对于 20 % 20\% 20% 的评测用例, n ≤ 50 n ≤ 50 n≤50 对于 100 % 100\% 100% 的评测用例, n ≤ 1 0 3 , a i ≤ 1 0 9 n ≤ 10^3,a_i≤10^9 n≤103,ai​≤109

思路

前缀和+set

#include #include #include #include using namespace std; typedef long long ll; ll n; ll num[1010] = {0}; ll pre[1010] = {0}; multiset mset; int main() { ios::sync_with_stdio(false); cout.tie(nullptr); cin >> n; for (int i = 1; i for (int r = l + 1; r for (int l = 0; l res = min(res, abs(*it - val)); } if (it != mset.begin()) { it--; res = min(res, abs(*it - val)); } } for (int i = r + 1; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有