关于HDU 1713 相遇周期

                


http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=3

 提前说明:正确代码下的测试数据:


经过多次测试与查找相关资料得:
题中给出的为周期,单位错了,应为(天/圈),当T1=a/b=5/1时,v1=(1圈/5天),即为1/5(圈每天)。T2=c/d=10/1时,v2=1/10,速度差为v1-v2=1/10( 圈/天),v1快,v2慢,把v2看为静止,v1以1/10 (圈/天)的速度跑圆,在10天后第一次相遇。。。
相遇周期计算公式推导:卫星的轨道周长都为1,假设2,行星与B卫星不动,则A与B的 相对速度为(1/Ta-1/Tb),所以A与B的相遇时间间隔是1/(1/Ta-1/Tb);
经过计算,本题实际上求a/b与c/d这两个分数的最小公倍数(关于为什么是最小公倍数,我也正在计算,明白以后会上传。),有一下两种思路
一:
思路: 对于两个最简的分数  a / b, c / d 把他们两个的最小公倍数 x / y 也设为一个分数形式,那么这个 x 一定能够整除 a , c, y 一定能够被 b , d整除。那么要求得最小公倍数,那么肯定是分子尽量小,即 a , c 的最小公倍数, 分母尽量大, 即 b , d 的最大公约数。
(转载)正确代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int gcd( int x, int y )
{
    if( y == 0 )
        return x;
    else
        return gcd( y, x % y );    
}

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

int main()
{
    int T;
    scanf( "%d", &T );
    while( T-- )
    {
        int a, b, c, d, rx, ry;
        scanf( "%d/%d", &a, &b );
        scanf( "%d/%d", &c, &d );
        int t = gcd( a, b );
        a /= t, b /= t;
        t = gcd( c, d ); 
        c /= t, d /= t; 
        if( gcd( b, d ) == 1 )
        {
            printf( "%d\n", lcm( a, c ) );
        }
        else
        {
            printf( "%d/%d\n", lcm( a, c ), gcd( b, d ) );
        }
    }
    return 0;
}
二:
思路:题目分析:题目输入c1/t1 c2/t2,,转换成:c1*t2/(t1*t2),  c2*t1/( t1*t2 ); 这时候我们只需要求出分子的最小公倍数k,然后k/( t1*t2 )就是题目求的周期
(转载)代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
__int64 gcd(__int64 a,__int64 b){
    __int64 c;
    if(a<b){
        c=a;    a=b;
        b=c;
    }
    while(b){
        c=b;
        b=a%b;
        a=c;
    }
    return a;
}
__int64 min_times(__int64 x1,__int64 x2){
    __int64 c=gcd(x1,x2);
    return x1*x2/c;
}
int main(){
    __int64 cas,c1,c2,t1;
    __int64 t2,p,k,m,n,h;
    cin>>cas;
    while(cas--){
        scanf("%I64d/%I64d",&c1,&t1);
                 //在这里提醒一下,用long long型的过不了!
                 //因为这个WA的好多次!
        scanf("%I64d/%I64dd",&c2,&t2);
        k=t1*t2;
        m=t2*c1;
        n=t1*c2;
        p=min_times(m,n);
        h=gcd(p,k);
        if(h==k){
            printf("%I64d\n",p/h);
        }
        else{
            printf("%I64d/%I64d\n",p/h,k/h);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值