← 查看本卷题目
组合题6 个小题
已作答 0 / 6
#include <algorithm>#include <cstdio>#include <cstring>#define ll long longint 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_checkcnt\_check 的值);“猜测正确”的含义为 assert_ans 函数 return true(执行第 25 行所在分支)的情况;所有输入保证 1≤k≤n1 \leq k \leq n)。

第26题单选题原卷 3 分

函数 guess2 在运行过程中,最多使用的猜测次数的量级为( )。

填写答案