HDOJ-1713-相遇周期

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
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define INT64 long long
 4 // to calculate the greatest common divisor
 5 INT64 GCD(INT64 a, INT64 b)
 6 {
 7     INT64 t;
 8     while (b) {
 9         t = a%b;
10         a = b;
11         b = t;
12     }
13     return a;
14 }
15 // to calculate the lowest common multiple
16 INT64 LCM(INT64 t1, INT64 t2)
17 {
18     return t1/GCD(t1,t2)*t2;
19 }
20 
21 int main()
22 {
23     int cs;
24     scanf("%d", &cs);
25     while (cs--) {
26         INT64 n1, n2, t1, t2, f1, f2;
27         scanf("%I64d/%I64d %I64d/%I64d", &n1,&t1,&n2,&t2);
28         INT64 lcm1, lcm2, gcd, gcd1, gcd2;
29         // reduce the input data to the simplest form
30         gcd1 = GCD(n1,t1);
31         gcd2 = GCD(n2,t2);
32         n1 /= gcd1;  
33         t1 /= gcd1;
34         n2 /= gcd2;
35         t2 /= gcd2;
36         // reduction of fractions to a common denominator
37         lcm1 = LCM(t1, t2);
38         f1 = lcm1/t1; // f1 = t2/GCD(t1,t2);
39         f2 = lcm1/t2; // f2 = t1/GCD(t1,t2);
40         lcm2 = LCM(n1*f1, n2*f2);
41         gcd = GCD(lcm1, lcm2);
42         if (lcm1/gcd == 1) {
43             printf("%I64d\n", lcm2/gcd);
44         }else {
45             printf("%I64d/%I64d\n", lcm2/gcd, lcm1/gcd);
46         }
47     }
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/superbin/archive/2012/11/04/2754146.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值