fork download
  1. #include <stdio.h>
  2.  
  3. #define ORIGINAL 0
  4. #define SOLUTION 1
  5.  
  6. void printb(unsigned x);
  7. int binsearch(int x, int v[], int n);
  8.  
  9.  
  10. int main(void){
  11. int i, n;
  12. unsigned int x = 0xFF30;
  13. int v[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  14.  
  15. for(i = 0; i <= 9; i++)
  16. printf("%d%4d\n", i, binsearch(i, v, 10));
  17.  
  18. return 0;
  19. }
  20.  
  21. #if ORIGINAL
  22. /*Èñõîäíûé âàðèàíò áèíàðíîãî ïîèñêà*/
  23. int binsearch(int x, int v[], int n){
  24. int low, high, mid;
  25.  
  26. low = 0;
  27. high = n - 1;
  28. while(low <= high){
  29. mid = (low + high) / 2;
  30. if(x < v[mid])
  31. high = mid - 1;
  32. else if(x > v[mid])
  33. low = mid + 1;
  34. else
  35. return mid;
  36. }
  37. return - 1;
  38. }
  39. #endif
  40.  
  41. #if SOLUTION
  42. /*Ðåøåíèå çàäà÷è 3.1*/
  43. int binsearch(int x, int v[], int n){
  44. int low, high, mid;
  45.  
  46. low = 0;
  47. high = n - 1;
  48. while(low < high){
  49. mid = (low + high) / 2;
  50. if(x <= v[mid])
  51. high = mid;
  52. else
  53. low = mid + 1;
  54. }
  55. if(v[high] == x && v[low] == x)
  56. return high;
  57. else
  58. return -1;
  59. }
  60. #endif
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
0   0
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9