fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main(void)
  6. {
  7. int ans, a;
  8. int low = 0, high = 99;
  9. int count = 0;
  10.  
  11. srand(time(0));
  12. ans = rand() % 100;
  13.  
  14. do {
  15. a = rand() % (high - low + 1) + low;
  16. count++;
  17.  
  18. printf("コンピュータの予想:%d\n", a);
  19.  
  20. if (a < ans) {
  21. printf("→ 小さい\n");
  22. low = a + 1;
  23. } else if (a > ans) {
  24. printf("→ 大きい\n");
  25. high = a - 1;
  26. }
  27.  
  28. } while (a != ans);
  29.  
  30. printf("正解!%d回で当てました\n", count);
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
コンピュータの予想:92
→ 大きい
コンピュータの予想:68
→ 大きい
コンピュータの予想:12
→ 小さい
コンピュータの予想:14
→ 小さい
コンピュータの予想:21
→ 大きい
コンピュータの予想:15
正解!6回で当てました