fork(2) download
  1. //面试考题3 吕德勤
  2. /*小王跟小李在玩一个猜数字游戏。首先小王选一个1到10的整数。然后,小李猜小王选了什
  3. 么数字,小王说小李猜的数字正确(“correct”)太大(“too high”)或者太小(“too low”),这
  4. 样重复直到“correct”为止。小李怀疑小王作弊,也就是说小王给的correct/too high/too low反
  5. 映有矛盾。
  6. 标准输入(stdin)有N场游戏的记录(N≥0),每两行表示小李的答案(1到10)跟小王的回复。
  7. 每场游戏最后都是“correct”。输入最后有一行“0”表示输入完毕。
  8. 提交的标准C或C++程序需要为每场游戏作出判断,游戏记录从标准输入读入。如果小王绝对
  9. 是作弊了(也就是说小王的反映有矛盾),输出一行“yes”到标准输出(stdout),否则输出一行
  10. “maybe”。*/
  11.  
  12. #include <iostream>
  13. #include <string>
  14.  
  15. using namespace std;
  16.  
  17. int number[10] = { 0 };
  18. int n;
  19. const char tooHigh[10] = "too high";
  20. const char tooLow[10] = "too low";
  21. const char correct[10] = "correct";
  22.  
  23. int strcmp(const char *str1, const char *str2);
  24. void doTooHigh(int x);
  25. void doTooLow(int x);
  26. void doCorrect(int x);
  27.  
  28. int main(void) {
  29. char reply[10];
  30. cin >> n;
  31. while (n != 0) {
  32. cin.getline(reply, 10);
  33. if (strcmp(reply, tooHigh) == 0)
  34. doTooHigh(n);
  35. else if (strcmp(reply, tooLow) == 0)
  36. doTooLow(n);
  37. else if (strcmp(reply, correct) == 0)
  38. doCorrect(n);
  39. cin >> n;
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. void doTooHigh(int x) {
  46. for (int i = n; i <= 10; i++)
  47. number[i] = 1;
  48. }
  49.  
  50. void doTooLow(int x) {
  51. for (int i = n; i > 0; i--)
  52. number[i] = 1;
  53. }
  54.  
  55. void doCorrect(int x) {
  56. if (number[n] == 0)
  57. cout << "maybe";
  58. else cout << "yes";
  59. for (int i = 0; i <= 10; i++)
  60. number[i] = 0;
  61. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
10
too high
3
too low
4
too high
2
correct
5
too low
7
too high
6
correct
0
compilation info
prog.cpp: In function 'void doCorrect(int)':
prog.cpp:60:16: warning: iteration 10u invokes undefined behavior [-Waggressive-loop-optimizations]
   number[i] = 0;
                ^
prog.cpp:59:2: note: containing loop
  for (int i = 0; i <= 10; i++)
  ^
/home/uLkvnt/ccDh9iLx.o: In function `main':
prog.cpp:(.text.startup+0x75): undefined reference to `strcmp(char const*, char const*)'
prog.cpp:(.text.startup+0x132): undefined reference to `strcmp(char const*, char const*)'
prog.cpp:(.text.startup+0x18a): undefined reference to `strcmp(char const*, char const*)'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty