正在载入在线练习界面,本页内容可直接阅读…
AK CSP › CSP-S 2025 第一轮真题 › 第23题
CSP-S 2025 第一轮 第23题:程序阅读第 2 题 · 第 2 小题
题目
#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%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 \leq k \leq n$)。本小题
不管输入的 $n$ 和 $k$ 具体为多少,$t=2$ 时的猜测数总是小于等于 $t=1$ 时的猜测数。
选项
- T. 正确
- F. 错误
答案
F
题解
选 F(错误)。找一个反例即可:令 \(n=3,\ k=1\)。
- \(t=1\):调用
check(1),因为 \(1\ge k\),直接猜对,猜测数为 1。 - \(t=2\):
- 先求出最小的 \(w\),使 \(w(w+1)/2\ge3\),得到 \(w=2\)。
- 调用
check(2),返回true,进入内层循环。 - 内层从
j = nh - ti + 1 = 1开始,调用check(1),返回true,随后assert_ans(1)猜对。猜测数为 2。
因此,\(t=2\) 时可能比 \(t=1\) 时猜测更多,题目中的“总是小于等于”不成立。
真题版权归 CCF 所有,本站仅用于非商业教学用途。页面加载后可直接在线作答,作答记录保存在本浏览器或账号中。 京ICP备2026056990号-1
京公网安备11010502062986号