Advertisement
kutuzzzov

Урок 6 Логарифмическая сложность

Nov 3rd, 2022
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename F>
  6. int FindFloor(int n, F drop) {
  7.     int a = 1;
  8.     int b = n;
  9.     while (a != b) {
  10.         int m = (a + b) / 2;
  11.         if (drop(m) == true) {
  12.             b = m;
  13.         } else {
  14.             a = m + 1;
  15.         }
  16.     }
  17.     return a;
  18. }
  19.  
  20. int main() {
  21.     int n,t;
  22.     cout << "Enter n and target floor number: "s << endl;
  23.     cin >> n >> t;
  24.  
  25.     int count = 0;
  26.     int found = FindFloor(n, [t, &count](int f) {
  27.         ++count;
  28.         return f >= t;
  29.     });
  30.  
  31.     cout << "Found floor "s << found << " after "s << count << " drops"s;
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement