测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A22464. #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("new check:%d\n", h); ++cnt_check; if…

判断题 较易

题目描述

#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("new 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 * (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;
}

当输入为 “6 5 1” 时,猜测次数为 5;当输入 “6 5 2” 时,猜测次数为 3。(    )

选项(单选)

上一题 下一题