shushus

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

Dec 25th, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 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)) {
  12.             b = m;
  13.         }else{
  14.             a = m+1;
  15.         }
  16.     }
  17.  
  18.     return a;
  19. }
  20.  
  21. int main() {
  22.     int n,t;
  23.     cout << "Enter n and target floor number: "s << endl;
  24.     cin >> n >> t;
  25.  
  26.     int count = 0;
  27.     int found = FindFloor(n, [t, &count](int f) {
  28.         ++count;
  29.         return f >= t;
  30.     });
  31.  
  32.     cout << "Found floor "s << found << " after "s << count << " drops"s;
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment