UVA906 Rational Neighbor【暴力】

As we know, finding a rational close to a given rational is straightforward. The minimal distance between two distinct integers is 1. By contrast, there is no minimal distance between two distinct rationals. A straightforward method for finding a rational close to a given rational a/b is based on the following construction. For every m > 0 one has a/b = (am)/(bm), and the neighbors (am ± 1)/(bm) lie at distance 1/(bm) from the given rational. So, by choosing m to be sufficiently large, one can make the distance to be as small as we please.
    Given a rational a/b and an upper bound n for the distance, the problem consists to find the rational c/d such that:
(i) a/b < c/d;
(ii) the distance between the rationals a/b and c/d is smaller or equal than n;
(iii) the denominator d is as small as possible.
Input
The input will contain several test cases, each of them consisting of two lines.
    The first line of the input contains two positive integers a and b which define the rational number a/b. The integers a and b are assumed to be in the interval [1, 100000]. The second line contain a positive real number n, 0.00000001 ≤ n ≤ 0.1, which gives the maximum distance allowed.
Output
For each test case, write to the output, on a line by itself, the two positive integers c and d which solve the problem.
Sample Input
96 145
0.0001
Sample Output
49 74

问题链接UVA906 Rational Neighbor
问题简述:(略)
问题分析
    简单数学题,用枚举法来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA906 Rational Neighbor */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int main()
{
    LL a, b, c, d;
    double n;
    while(~scanf("%lld%lld", &a, &b)) {
        scanf("%lf", &n);
        LL gcdab = __gcd(a, b);
        a /= gcdab;
        b /= gcdab;
        for(d = 1; ;d++) {
            c = d * a / b;
            while(b * c <= a * d) c++;
            if(b * c - a * d <= n * b * d) {
                printf("%lld %lld\n", c, d);
                break;
            }
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值