fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int search(int *a, int n, int x){
  5. int mid, low=0, high=n-1;
  6. while ((x>=a[low])&&(x<=a[high])){
  7. mid=low+((x-a[low])*(high-low)/(a[high]-a[low]));
  8. if(a[mid]<x){
  9. low=mid+1;
  10. }
  11. else if(x<a[mid]){
  12. high=mid-1;
  13. }
  14. else return mid;
  15. }
  16. if(x==a[low]){
  17. return low;
  18. }
  19. else return -1;
  20. }
  21. int main() {
  22. // your code goes here
  23. int a[] = {1,1,1,1,1,1,1,1,1,1};
  24. cout << search(a,sizeof(a)/sizeof(*a),1);
  25. return 0;
  26. }
Runtime error #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
Standard output is empty