dzungchaos

C++ "TÌm kiếm nhị phân"

Nov 6th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. // Vi du ve bai toan tim kiem nhi phan
  2.  
  3. #include <iostream>
  4. using namespace std;
  5. float x;
  6.  
  7. float f(float x){
  8.     return x*x*x + x - 1;
  9. }
  10.  
  11. int main(){
  12.     float l = -5, r = 5, eps = 1e-5;
  13.     while(r - l > eps){
  14.         float mid = (r + l) / 2;
  15.         if (f(mid) > 0) r = mid;
  16.         else l = mid;
  17.     }
  18.     cout << "Nghiem la " << (r + l) / 2 << endl;
  19.     return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment