ACMSTEP 2.1.3 相遇周期 //数论 分数的最小公倍数

原题链接     同题:HDU 1713

输入c1/t1 c2/t2 ,也就是速度的分数形式,转换成:c1*t2/(t1*t2),  c2*t1/( t1*t2 );
需要求出分子的最小公倍数k,然后k/( t1*t2 )就是题目求的周期

注意用 __int64, 否则WA

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

__int64 gcd(__int64 a,__int64 b)
{
	if (!a || !b) return a>b?a:b;
	for (int t; t = x% y; x =y; y = t);
	return y;
}
int main()
{
	int t;
	__int64 a,b,c,d;
	cin>>t;
	while(t--)
	{
	scanf("%I64d/%I64d%I64d/%I64d",&a,&b,&c,&d);
	a=a*d*c*b/gcd(a*d,c*b);
	b=b*d;
	if(a%b)
	{
		c=gcd(a,b);
		printf("%I64d/%I64d\n",a/c,b/c);
	}
	else
		printf("%I64d\n",a/b);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值