UVa 10673 - Play with Floor and Ceil

来源:互联网 发布:oracle数据还原 编辑:程序博客网 时间:2024/05/15 05:07

扩展欧几里得算法,直接套模板即可。

代码如下:

#include <cstdio>#include <cmath>#include <cstring>void gcd(int a, int b, long long &d, long long &x, long long &y){    if(!b)    {        d = a;        x = 1;        y = 0;    }    else    {        gcd(b, a%b, d, y, x);        y -= x*(a/b);    }}int main(){#ifdef test    freopen("sample.txt", "r", stdin);#endif    int a, b, t;    long long p, q, ans;    double x, k;    scanf("%d", &t);    while(t--)    {        scanf("%lf%lf", &x, &k);        a = floor(x/k);        b = ceil(x/k);        gcd(a, b, ans, p, q);        ans = (int)x / ans;        printf("%lld %lld\n", p*ans, q*ans);    }    return 0;}


原创粉丝点击