#2562. 2025 CSP-S1 真题 · 阅读程序2
2025 CSP-S1 真题 · 阅读程序2
2025 CSP-S1 真题(120 分钟,总分 100 分)
阅读程序2(共 6 题,共 13.5 分)
1.
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
int cnt_broken = 0;
int cnt_check = 0;
int n, k;
inline bool check(int h) {
printf("now check:%d\n", h);
++cnt_check;
if (cnt_broken == 2) {
printf("You have no egg!\n");
return false;
}
if (h >= k) {
++cnt_broken;
return true;
} else {
return false;
}
}
inline bool assert_ans(int h) {
if (h == k) {
printf("You are Right using %d checks\n", cnt_check);
return true;
} else {
printf("Wrong answer!\n");
return false;
}
}
inline void guess1(int n) {
for (int i = 1; i <= n; ++i) {
if (check(i)) {
assert_ans(i);
return;
}
}
}
inline void guess2(int n) {
int w = 0;
for (w = 1; w * (w + 1) / 2 < n; ++w)
;
for (int ti = w, nh = w; --ti, nh += ti, nh = std::min(nh, n)) {
if (check(nh)) {
for (int j = nh - ti + 1; j < nh; ++j) {
if (check(j)) {
assert_ans(j);
return;
}
}
assert_ans(nh);
return;
}
}
}
int main() {
scanf("%d", &n, &k);
int t;
scanf("%d", &t);
if (t == 1) {
guess1(n);
} else {
guess2(n);
}
return 0;
}
(注意:下述的"猜测数"为调用 check 函数的次数 (即 cnt_check 的值);"猜测正确"的含义为 assert_ans 函数 return true (执行第 25 行所在分支)的情况;所有输入保证 1 ≤ k ≤ n。)
当输入为"6 5 1"时,猜测次数为5;当输入为"6 5 2"时,猜测次数为3。
{{ select(1) }}
- 正确
- 错误
2. 不管输入的n和k具体为多少,t=2时的猜测数总是小于等于t=1时的猜测数。
{{ select(2) }}
- 正确
- 错误
3. 不管t=1或t=2,程序都一定会得到正确结果。
{{ select(3) }}
- 正确
- 错误
4. 函数guess1在运行过程中,cnt_broken的值最多为()。
{{ select(4) }}
- 0
- 1
- 2
- n
5. 函数guess2在运行过程中,最多使用的猜测次数的量级为()。
{{ select(5) }}
- O(n)
- O(n²)
- O(\sqrt{n})
- O(log n)
6. 当输入的n=100的时候,代码中t=1和t=2分别需要的猜测次数最多分别为()。
{{ select(6) }}
- 100, 14
- 100, 13
- 99, 14
- 99, 13
相关
在下列比赛中: