铺瓷砖

这里写图片描述

问题的实质是要我们求两个分数的最小公倍数。
首先,我们要知道,整数a和b的最小公倍数是a*b/gcd(a,b);
那么怎样来求分数的最小公倍数呢?
我们可以先将两个分数通分,分母变为t1,然后再求分子的最小公倍数t2。
那么答案就是t2/t1, 注:最后要约分.

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
LL a,b,c,d,T;
LL gcd(LL x,LL y)
{
    if(y==0) return x;
    return gcd(y,x%y);
}
int main()
{
    scanf("%lld",&T);
    while(T--)
    {
        scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
        LL t1=gcd(b,d);
        t1=b*d/t1;//分母 
        a=t1/b*a;c=t1/d*c;b=d=t1;
        LL t2=gcd(a,c);
        t2=a*c/t2;//分子 
        if(t2%t1==0) printf("%lld
",t2/t1);
        else{
            LL t3=gcd(t1,t2);
            printf("%lld/%lld
",t2/t3,t1/t3);
        }   
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dfsac/p/7587830.html