您的位置:首页 > 其它

HDU 相遇周期

2016-01-11 23:25 295 查看
HDU -


相遇周期

/*
Problem :相遇周期
Author  : herongwei
Language : G++
CreateTime: 2016/01/11
*/

/*解析:两周期分别为a/b、c/d,既然是相遇周期,就是最早什么时间相遇,
也就是两者的最小公倍数,转化一下,也就是求LCM(a, c)/ gcd(b, d)。直接求即可,
不一定要用long long 或者 __int64,int也能过。注意:先把a/b和c/d尽可能的化简再求。
*/
#include <math.h>
#include <queue>
#include <deque>
#include <vector>
#include <stack>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
const double eps = 1e-6;
const double Pi = acos(-1.0);
static const int inf= 0x3f3f3f3f;
static const int maxn = 1e5+10;
typedef long long LL;
int m, n;
char ss[1005][1005];
typedef  pair<int, int> P;
int ans[1003];
int cnt;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
int main()
{
   // freopen("1.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
         int a1,b1,a2,b2;
         scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2);
         int g1=gcd(a1,b1);
         int g2=gcd(a2,b2);
         a1/=g1;b1/=g1;
         a2/=g2;b2/=g2;
         int c1=lcm(a1,a2);
         int c2=gcd(b1,b2);
         int c3;
         if(c1%c2==0) printf("%d\n",c1/c2);
         else {
            int g3=gcd(c1,c2);
            c1/=g3;
            c2/=g3;
            printf("%d/%d\n",c1,c2);
         }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: