fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int T[20] = {3, 4, 5, 8, 11, 13, 19, 20, 23, 26, 29, 30, 31, 33, 34, 37, 38, 44, 45, 48};
  5. int x;
  6. int L = 0;
  7. int H = 19;
  8. int M;
  9.  
  10. printf("検索したい数値を入力してください: ");
  11. scanf("%d", &x);
  12.  
  13. M = (L + H) / 2;
  14. while ((L <= H) && (x != T[M])) {
  15. if (x < T[M]) {
  16. H = M - 1;
  17. } else {
  18. L = M + 1;
  19. }
  20. M = (L + H) / 2;
  21. }
  22.  
  23. if (x == T[M]) {
  24. printf("Found at %d\n", M + 1);
  25. } else {
  26. printf("Not found\n");
  27. }
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 5320KB
stdin
5
stdout
検索したい数値を入力してください: Found at 3