#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;
}