HDU ACM Steps: meet cycle

HDU ACM Steps: meet cycle

Title Description

Topics were cut (involving politics, I do not give too ....)

Satellite is an important tool for the detection of these, our problem is a known operating cycle of the two satellites, seeking to meet their cycle.

Entry

The first line of the input data T a positive integer, denotes the number of sets of test data then is T test cases. Each test contains two positive integers, separated by spaces. Each comprising two positive integers, the number of days required revolutions n turns (26501/6335, 26501 represents the transfer ring to 6335 days), with '/' apart.

Export

For each test, they encounter the output period, if the period is an integer met by the integer, or exact-fraction expressed.

SAMPLE INPUT

2
26501/6335 18468/42
29359/11479 15725/19170

Sample Output

81570078/7
5431415

Thinking

1. meet cycle is the least common multiple of two cycles of
the least common multiple of 2. Integer everyone is familiar with, and here is a fraction of the least common multiple. Seeking the least common multiple fractions: first fraction of about two minutes, two molecules of the least common multiple as ask molecule, two denominator common divisor as the denominator request, the request is the least common multiple of the numerator and denominator minutes to about
3. Tips : find the least common multiple and greatest common divisor function can be used as a template

Code

#include<stdio.h>
typedef long long LL;

LL n,m;
LL x1,y1,x2,y2;

LL gcd(LL x,LL y)
{
     return y ? gcd(y,x%y) : x;
}

LL lcm(LL x,LL y)
{
	return x*y/gcd(x,y);
}

int main()
{
	scanf("%d",&n);
	while(n--)
	{
		scanf("%lld/%lld",&x1,&y1);
		scanf("%lld/%lld",&x2,&y2);
		
		/*约分*/
		m=gcd(x1,y1);
		x1/=m;y1/=m;
		
		m=gcd(x2,y2);
		x2/=m;y2/=m;
		
		
		x1=lcm(x2,x1);//求分子
		y1=gcd(y1,y2);// 求分母
		
		m=gcd(x1,y1);
		x1/=m;y1/=m;
		
		y1==1 ? printf("%lld\n",x1) : printf("%lld/%lld\n",x1,y1);
	}
	return 0;
 } 

template

typedef long long LL;
LL gcd(LL x,LL y)
{
     return y ? gcd(y,x%y) : x;
}

LL lcm(LL x,LL y)
{
	return x*y/gcd(x,y);
}
Published 13 original articles · won praise 2 · Views 327

Guess you like

Origin blog.csdn.net/weixin_45718149/article/details/104559663
Recommended