51Nod 1187 寻找分数
Posted widsom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 << "\n";
#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\n", p, q);
return 0;
以上是关于51Nod 1187 寻找分数的主要内容,如果未能解决你的问题,请参考以下文章