• 51Nod 1187 寻找分数


    1187 寻找分数
    思路:类欧几里得
    代码:

    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize(4)
    #include<bits/stdc++.h>
    using namespace std;
    #define y1 y11
    #define fi first
    #define se second
    #define pi acos(-1.0)
    #define LL long long
    //#define mp make_pair
    #define pb push_back
    #define ls rt<<1, l, m
    #define rs rt<<1|1, m+1, r
    #define ULL unsigned LL
    #define pll pair<LL, LL>
    #define pli pair<LL, int>
    #define pii pair<int, int>
    #define piii pair<pii, int>
    #define pdd pair<double, double>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //head
    
    int T;
    LL a, b, c, d, p, q;
    //a/b < p/q < c/d
    void solve(LL a, LL b, LL c, LL d) {
        if(a == 0) {
            p = 1;
            q = d/c+1;
            return ;
        }
        if(a >= b) {
            solve(a%b, b, c-(a/b)*d, d);
            p += (a/b)*q;
            return ;
        }
        if(c > d) {
            p = q = 1;
            return ;
        }
        solve(d, c, b, a);
        swap(p, q);
    }
    int main() {
        scanf("%d", &T);
        while(T--) {
            scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
            solve(a, b, c, d);
            printf("%lld/%lld
    ", p, q);
        }
        return 0;
    }
    
  • 相关阅读:
    gevent
    pymongo的数组操作
    pymongo的聚合操作
    python操作redis的情况总结
    协程
    3
    6
    10
    4
    5
  • 原文地址:https://www.cnblogs.com/widsom/p/11345230.html
Copyright © 2020-2023  润新知