Guest User

InnopolisB

a guest
Dec 19th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. const long double inf = 9e18;
  8. const long double eps = 1e-16;
  9.  
  10. long double a, b;
  11.  
  12. long double f (int x, int y) {
  13.         return fabs (x * 1.0 / y - a / b);
  14. }
  15. bool eq (long double a, long double b) {
  16.         return fabs (a - b) < eps;
  17. }
  18. int main () {
  19.         freopen ("approximate.in", "r", stdin);
  20.         freopen ("approximate.out", "w", stdout);
  21.         int n;
  22.         cin >> a >> b >> n;
  23.         int l = 0;
  24.         int ans = 0;
  25.         long double mn = inf;
  26.         for (int i = 1; i <= n; i++) {
  27.                 while (l < i && f (l + 1, i) < f (l, i)) {
  28.                         l++;                        
  29.                 }
  30.                 if (f (l, i) < mn) {
  31.                         ans += 1 + eq (f (l + 1, i), f (l, i));
  32.                         mn = f (l, i);
  33.                 }
  34.         }
  35.         cout << ans << endl;
  36.         return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment