您的位置:首页 > 其它

HDOJ-1713-相遇周期

2012-11-04 20:21 211 查看
1. 题目中的Input描述是错的,正确解释为:输入n/t,表示转t圈需要n天。相遇周期T = 1 / fabs(t1/n1 - t2/n2),即:周期 = 路程差 / 速度差

2. WA若干次,在Discuss中了解到,64-bit int在windows系统和Linux系统中输入输出格式是 I64d 还是 lld 的问题。在我的系统上用Dev C++测试后两种格式都正确。说明与编译器的版本和系统版本有关。这里提交选择G++/GCC则需要用 I64d 格式,否则,会WA。

3. 如果计算过程中尽量避免连续相乘操作,可用32-bit int版本AC。[见此题Discuss]

View Code

#include <stdio.h>
#include <string.h>
#define INT64 long long
// to calculate the greatest common divisor
INT64 GCD(INT64 a, INT64 b)
{
INT64 t;
while (b) {
t = a%b;
a = b;
b = t;
}
return a;
}
// to calculate the lowest common multiple
INT64 LCM(INT64 t1, INT64 t2)
{
return t1/GCD(t1,t2)*t2;
}

int main()
{
int cs;
scanf("%d", &cs);
while (cs--) {
INT64 n1, n2, t1, t2, f1, f2;
scanf("%I64d/%I64d %I64d/%I64d", &n1,&t1,&n2,&t2);
INT64 lcm1, lcm2, gcd, gcd1, gcd2;
// reduce the input data to the simplest form
gcd1 = GCD(n1,t1);
gcd2 = GCD(n2,t2);
n1 /= gcd1;
t1 /= gcd1;
n2 /= gcd2;
t2 /= gcd2;
// reduction of fractions to a common denominator
lcm1 = LCM(t1, t2);
f1 = lcm1/t1; // f1 = t2/GCD(t1,t2);
f2 = lcm1/t2; // f2 = t1/GCD(t1,t2);
lcm2 = LCM(n1*f1, n2*f2);
gcd = GCD(lcm1, lcm2);
if (lcm1/gcd == 1) {
printf("%I64d\n", lcm2/gcd);
}else {
printf("%I64d/%I64d\n", lcm2/gcd, lcm1/gcd);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: