fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int lower_bound(int d, int a[], int n)
  5. {
  6. int l=0, r=n;
  7. while (l < r)
  8. {
  9. int m = (l+r)/2; //Important to floor the middle, otherwise it will not work when l+1=r
  10. if (a[m] < d)
  11. l = m + 1;
  12. else
  13. r = m;
  14. }
  15. return l;
  16. }
  17.  
  18. int main() {
  19. // your code goes here
  20. int a[] = {10, 20};
  21. int d;
  22. while (cin >> d)
  23. {
  24. cout << lower_bound(d, a, (sizeof(a))/(sizeof(a[0]))) << "\n";
  25. }
  26. return 0;
  27. }
Success #stdin #stdout 0s 3100KB
stdin
5
10
20
30
stdout
0
0
1
2