• 求教奥数题:张明骑自行车,速度为每小时14千米,王华骑摩托车,速度为每小时35千米,他们分别从A、B两地出发,并在两地之间不断往返行驶,且两人第四次相遇(两人同时到达同一地点叫做相遇)与第五次相遇的地


    求教奥数题:张明骑自行车,速度为每小时14千米,王华骑摩托车,速度为每小时35千米,他们分别从A、B两地出发,并在两地之间不断往返行驶,且两人第四次相遇(两人同时到达同一地点叫做相遇)与第五次相遇的地点恰好相距120千米,那么,A、B两地只见的距离是多少千米?
    
    
    图解:相遇和追击其实都是函数的交点(这小学学过?)
    
    
    
    
    
    
    
    程序员解:
    #include <iostream>
    #include <iomanip> 

    using namespace std;

    //speedA>=speedB
    int speedA = 35;
    int speedB = 14;

    struct TEncounter
    {
    double encounterPercent;
    int directionA;//1 right -1 left
    int directionB;
    };

    TEncounter GetnexTEncounter(int n,TEncounter lastEncounter)
    {
    TEncounter nexTEncounter;
    if (n == 0)
    {
    //第一次遭遇
    nexTEncounter.encounterPercent = 1.0f*speedA/(speedA+speedB);
    nexTEncounter.directionA = 1;
    nexTEncounter.directionB = -1;
    return nexTEncounter;
    }
    if (lastEncounter.directionA == 1 && lastEncounter.directionB == 1)
    {
    //都往右,追是追不上了,堵
    nexTEncounter.encounterPercent = lastEncounter.encounterPercent + (1 - lastEncounter.encounterPercent)*2*speedB/(speedA+speedB);
    nexTEncounter.directionA = -1;
    nexTEncounter.directionB = 1;
    }
    else if (lastEncounter.directionA == -1 && lastEncounter.directionB == -1)
    {
    //都往左,跟都往又一样
    nexTEncounter.encounterPercent = lastEncounter.encounterPercent - lastEncounter.encounterPercent*2*speedB/(speedA+speedB);
    nexTEncounter.directionA = 1;
    nexTEncounter.directionB = -1;
    }
    else if (lastEncounter.directionA == -1 && lastEncounter.directionB == 1)
    {
    if ((1-lastEncounter.encounterPercent)/speedB > (1+lastEncounter.encounterPercent)/speedA)
    {
    //追上了
    nexTEncounter.encounterPercent = lastEncounter.encounterPercent + 2*lastEncounter.encounterPercent*speedB/(speedA-speedB);
    nexTEncounter.directionA = 1;
    nexTEncounter.directionB = 1; 
    }
    else
    {
    //追不上就堵
    nexTEncounter.encounterPercent = 1 - (2.0f/(speedA+speedB)-(1-lastEncounter.encounterPercent)/speedB)*speedB;
    nexTEncounter.directionA = 1;
    nexTEncounter.directionB = -1;
    }
    }
    else if (lastEncounter.directionA == 1 && lastEncounter.directionB == -1)
    {
    if (lastEncounter.encounterPercent/speedB > (2-lastEncounter.encounterPercent)/speedA)
    {
    //追
    nexTEncounter.encounterPercent = lastEncounter.encounterPercent - 2*(1-lastEncounter.encounterPercent)*speedB/(speedA-speedB);
    nexTEncounter.directionA = -1;
    nexTEncounter.directionB = -1;
    }
    else
    {
    ////追不上,堵
    nexTEncounter.encounterPercent = lastEncounter.encounterPercent - (2.0f/(speedA+speedB)-(1-lastEncounter.encounterPercent)*2/speedA)*speedA;
    nexTEncounter.directionA = -1;
    nexTEncounter.directionB = 1;
    }
    }
    if (nexTEncounter.encounterPercent == 1 && lastEncounter.encounterPercent == 1)
    {
    //终极遭遇
    return GetnexTEncounter(n,nexTEncounter);
    }
    return nexTEncounter;
    }

    int main()
    {
    cout.setf(ios::fixed);
    TEncounter encounter[20];
    encounter[0] = GetnexTEncounter(0,encounter[0]);
    for (int i = 1; i < 20; i++)
    {
    encounter[i] = GetnexTEncounter(i,encounter[i-1]);
    }
    for (int i = 0; i < 20; i++)
    {
    cout<<setprecision(4)<<encounter[i].encounterPercent<<"\tA direction:"<<encounter[i].directionA<<"\tB direction:"<<encounter[i].directionB<<endl;
    }
    cout<<endl<<"AB="<<120.0f/(encounter[4].encounterPercent - encounter[3].encounterPercent)<<endl;
    return 0;
    }
    
    
  • 相关阅读:
    MongoDB笔记: 安装和常见问题
    Spring Boot方式的Dubbo项目
    Centos7安装Redis5.0.5并加入Systemd服务
    生成Nginx服务器SSL证书和客户端证书
    OpenSSL的证书, 私钥和签名请求(CSRs)
    迁移Git项目到Gitlab
    Ubuntu18.04 Server安装Nginx+Git服务和独立的svn服务
    ESXi6.5上的Ubuntu虚机在远程SSH时宕机
    Nginx访问路径添加密码保护
    从阿里云DATAV GeoAtlas接口抽取行政区划数据
  • 原文地址:https://www.cnblogs.com/marryZhan/p/2213908.html
Copyright © 2020-2023  润新知