Advertisement
Dizzy3113

Untitled

Jan 15th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n, t[100], c=0;
  5. float v;
  6. void caut(int s, int d)
  7. {
  8. if(s==d)
  9. {
  10. if(v==t[s])
  11. {
  12. cout<<"Gasit pe pozitia: "<<s;
  13. c++;
  14. }
  15.  
  16. }
  17. else if(s<d)
  18. {
  19. int m=(s+d)/2;
  20. if(v==t[m])
  21. {
  22. cout<<"Gasit pe pozitia: "<<m;
  23. c++;
  24. }
  25.  
  26. else if(v<t[m])
  27. {
  28. caut(s, m-1);
  29. c++;
  30. }
  31. else
  32. {
  33. caut(m+1, d);
  34. c++;
  35. }
  36.  
  37. }
  38. else
  39. cout<<"nu exista";
  40.  
  41. }
  42. int main()
  43. {
  44. cin>>n>>v;
  45. for(int i=1; i<=n; i++)
  46. cin>>t[i];
  47.  
  48. caut(1, n);
  49. cout<<endl<<"Nr comparari: "<<c;
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement