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 }

 

posted @ 2012-11-04 20:21  superbin  阅读(323)  评论(0编辑  收藏  举报