1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #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); int nh = w; for (int ti = w; ti >= 1; --ti, nh += ti) { if (nh > n) nh = n; if (check(nh)) { for (int j = nh - ti + 1; j < nh; ++j) { if (check(j)) { assert_ans(j); return; } } // 如果上面没返回,则 nh - ti + 1 就是答案 assert_ans(nh - ti + 1); return; } } // 如果一直没有鸡蛋破碎 assert_ans(n); 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≤k≤n。)
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、当输入为“65 1”时,猜测次数为5;当输入“65 2”时,猜测次数为3。
2、不管输入的 n 和 k 具体为多少,t=2 时的猜测数总是小于等于 t=1 时的猜测数。
3、不管 t=1 或 t=2,程序都一定会得到正确结果。
4、函数 guess1 在运行过程中,cnt_broken 的值最多为( )。
5、函数 guess2 在运行过程中,最多使用的猜测次数的量级为( )。
6、当输入的 n=100 时,代码中 t=1 和 t=2 分别要多少次数最多分别为( )。
