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;
}


Cu1
发布了30 篇原创文章 · 获赞 2 · 访问量 954

猜你喜欢

转载自blog.csdn.net/CUCUC1/article/details/104973311