Arab Collegiate Programming Contest 2015(D)

It's 2050. Humans have already colonized Mars and other planets long time ago and there are alreadysome programs for travelling to the other galaxies using wormholes. Scientists are currently studying themysteries of the black holes. Their observations concluded that everything we know about physics andmathematics is completely different inside the black hole. For example, do you remember the greatestcommon divisor (GCD) and the lowest common multiple (LCM)? These functions are normally definedonly on integers. The situation is different inside the black hole; GCD and LCM are also defined onrational numbers. For two rational numbers a/b and c/d: their GCD is the greatest rational number thatdivides both numbers to an integer, and their LCM is the lowest rational number that both numbersdivide to an integer. For example, GCD(1/2, 1/3) = 1/6 and LCM(1/2, 1/3) = 1/1. Can you help thescientists in their missions solving out the mysteries of the black holes? Given two rational numbers, findtheir GCD and LCM inside the black hole.
Input
Your program will be tested on one or more test cases. The first line of the input will be a single integer T,the number of test cases (1 ≤ T ≤ 1000). Followed by T test cases. Each test case contains four integersa, b, c, and d (1 ≤ a, b, c, d ≤ 2 × 10^9) representing the two rational numbers a/b and c/d.
Output
For each test case, print a single line containing two rational numbers m/n and x/y, the GCD and theLCM of the two given rational numbers. m/n and x/y must be in their simplest form. In other words, theGCD(m,n) and GCD(x,y) must be 1. 
样例输入 
复制
2
1 2 1 3
1 5 1 7
样例输出 
复制
1/6 1/1
1/35 1/1
编辑代码

其实应该,把分子分母GCD求出来之后,直接化最简再利用分数的GCD和LCM。不是求i出来答案之后在约分。。留个纪念,英语不好。

#include <iostream>
#define ll long long

using namespace std;

ll GCD(ll a, ll b)
{
	while(b)
	{
		ll temp = a % b;
		a = b;
		b = temp;
	}
	return a;
}
int main()

{
	ll T,a,b,c,d,x,y;
	cin >> T;
	while(T--)
	{
		cin>>a>>b>>c>>d;
		ll k = GCD(a, b);
		a/=k;
		b/=k;
		k=GCD(c, d);
		c/=k;
		d /=k;
		cout<< GCD(a,c)<<'/'<<b*d/GCD(b,d)<<' '<<a*c/GCD(a,c)<<'/'<<GCD(b,d)<<endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值