fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<assert.h>
  4.  
  5. void _binary_search(unsigned int,unsigned int,unsigned int,int*);
  6.  
  7. int main(int argc,const char*argv[]) {
  8. unsigned int test,start,end,i;
  9. int _rec_depth;
  10. fscanf(stdin,"%d",&test);
  11. assert(test>0 && test<1001);
  12. while(test--) {
  13. _rec_depth = 0;
  14. fscanf(stdin,"%d%d%d",&start,&end,&i);
  15. assert((start>=0 && start<1001) && (end>0 && end<1001) && (i>0 && i<1001));
  16. if(!(start == end)) _binary_search(start,end,i,&_rec_depth);
  17. fprintf(stdout,"%d\n",_rec_depth);
  18. }
  19. return 0;
  20. }
  21.  
  22. void _binary_search(unsigned int start,unsigned int end,unsigned int i,int*_rec_depth) {
  23. unsigned int mid;
  24. if(i>=start && i<=end) {
  25. while(start<end) {
  26. (*_rec_depth)++;
  27. fprintf(stdout,"%d %d\n",start,end);
  28. mid = ((end-start)/2) + start;
  29. if(mid>=i) end = mid;
  30. else start = mid+1;
  31. }
  32. if((start == i) && (end == i)) {
  33. (*_rec_depth)++;
  34. fprintf(stdout,"%d %d\n",start,end);
  35. }
  36. }
  37. else *_rec_depth = -1;
  38. }
Success #stdin #stdout 0s 9424KB
stdin
4
0 4 3
7 15 14
1 16 9
7 14 3
stdout
0 4
3 4
3 3
3
7 15
12 15
14 15
14 14
4
1 16
9 16
9 12
9 10
9 9
5
-1