- 月赛
【ZCX-001-DIV3】
- @ 2026-3-25 9:31:06
题解:水果蛋糕
思路
每个蛋糕需要:
- 3 个草莓
- 2 个蓝莓
因此可以分别计算:
- 草莓最多能做:x / 3
- 蓝莓最多能做:y / 2
注意这里是整数除法(向下取整)。
最终答案就是两者的最小值:
min(x / 3, y / 2)
向下取整说明
在本题中:
- x / 3 表示最多能用草莓做多少个蛋糕
- y / 2 表示最多能用蓝莓做多少个蛋糕
这里必须使用向下取整,原因是:
只能做完整蛋糕,不能用不够的材料拼一个
例如:
- 7 个草莓,每个蛋糕需要 3 个
- 只能做 2 个(剩 1 个没用)
举例
输入: 7 4
计算:
7 / 3 = 2
4 / 2 = 2
答案: 2
代码
C++ 实现
#include <bits/stdc++.h>
using namespace std;
int main() {
long long x, y;
cin >> x >> y;
cout << min(x / 3, y / 2);
return 0;
}
题解:土豆服务器
思路
题目要求筛选满足以下条件的土豆:
- 含糖量:x ≥ 500
- 价格:p < 500
只需要遍历所有土豆:
- 如果满足条件,就:
- 数量 +1
- 总价格累加
算法步骤
- 读入 n
- 初始化:
- cnt = 0(数量)
- sum = 0(总价格)
- 遍历每个土豆:
- 若 x >= 500 且 p < 500
- cnt++
- sum += p
- 若 x >= 500 且 p < 500
- 输出 cnt sum
代码
C++ 实现
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int cnt = 0;
int sum = 0;
while (n--) {
int x, p;
cin >> x >> p;
if (x >= 500 && p < 500) {
cnt++;
sum += p;
}
}
cout << cnt << " " << sum;
return 0;
}
题解:C破解等式
思路
题目给出等式:
a / x + b * y = c
要求统计满足条件的正整数对 (x, y) 数量。
最直接的方法是暴力枚举所有可能的 (x, y):
- 枚举 x
- 枚举 y
- 判断是否满足等式
枚举范围
为了减少无效枚举:
- x:1 到 a
- y:1 到 c / b
判断条件
对于每一对 (x, y),需要满足:
- a % x == 0
- a / x + b * y == c
满足则答案加一。
代码(C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int a, b, c;
cin >> a >> b >> c;
int ans = 0;
for (int x = 1; x <= a; x++) {
if (a % x != 0) continue;
for (int y = 1; y <= c / b; y++) {
if (a / x + b * y == c) {
ans++;
}
}
}
cout << ans << '\n';
}
return 0;
}
D 期望与方差
思路
根据题目定义直接计算。
设:
期望:
方差:
$\mu_2 = \frac{(x_1-E)^2p_1 + (x_2-E)^2p_2 + \dots + (x_n-E)^2p_n}{q}$
做法
-
计算:
-
计算期望:
- 再计算方差:
$\mu_2 = \frac{(x_1-E)^2p_1 + (x_2-E)^2p_2 + \dots + (x_n-E)^2p_n}{q}$
代码(C++)
#include <bits/stdc++.h>
using namespace std;
int x[100010], p[100010];
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n; i++) cin >> p[i];
double q = 0, sum = 0;
for (int i = 0; i < n; i++) {
q += p[i];
sum += x[i] * p[i];
}
double E = sum / q;
double var = 0;
for (int i = 0; i < n; i++) {
var += (x[i] - E) * (x[i] - E) * p[i];
}
var /= q;
cout << fixed << setprecision(6) << E << '\n' << var << '\n';
return 0;
}
0 条评论
目前还没有评论...