组合题6 个小题
已作答 0 / 6
1#include <algorithm>2#include <cstdio>3#include <cstring>4#define ll long long5int cnt_broken = 0;6int cnt_check = 0;7int n, k;8inline bool check(int h) {9 printf("now check:%d\n", h);10 ++cnt_check;11 if (cnt_broken == 2) {12 printf("You have no egg!\n");13 return false;14 }15 if (h >= k) {16 ++cnt_broken;17 return true;18 } else {19 return false;20 }21}22inline bool assert_ans(int h) {23 if (h == k) {24 printf("You are Right using %d checks\n", cnt_check);25 return true;26 } else {27 printf("Wrong answer!\n");28 return false;29 }30}31inline void guess1(int n) {32 for (int i = 1; i <= n; ++i) {33 if (check(i)) {34 assert_ans(i);35 return;36 }37 }38}39inline void guess2(int n) {40 int w = 0;41 for (w = 1; w * (w + 1) / 2 < n; ++w)42 ;43 for (int ti = w, nh = w;; --ti, nh += ti, nh = std::min(nh, n)) {44 if (check(nh)) {45 for (int j = nh - ti + 1; j < nh; ++j) {46 if (check(j)) {47 assert_ans(j);48 return;49 }50 }51 assert_ans(nh);52 return;53 }54 }55}56int main() {57 scanf("%d%d", &n, &k);58 int t;59 scanf("%d", &t);60 if (t == 1) {61 guess1(n);62 } else {63 guess2(n);64 }65 return 0;66}注意:下述的“猜测数”为调用 check 函数的次数(即 cnt_check 的值);“猜测正确”的含义为 assert_ans 函数 return true(执行第 25 行所在分支)的情况;所有输入保证 1≤k≤n)。
函数 guess2 在运行过程中,最多使用的猜测次数的量级为( )。