15,447
社区成员




/*
*环行跑道周长为400米.甲乙两人同时同地顺时针沿环行跑道跑步.甲52米/分钟.
*乙46米/分钟.假设甲乙两人每跑100米停下休息一分钟.问甲多长时间追上乙?
*/
struct Accuracy_Control
{
static const long double time_acc = 1E-2;
static const long double position_acc = 5.0E-3;
};
struct Racetrack
{
static const long double circumference = 400.0L;
};
struct Runner
{
bool is_running;
long double speed;
long double position;
long double status_remain_time;
long double time;
Runner():
is_running(false),
speed(0.0L),
position(0.0L),
status_remain_time(0.0L),
time(0.0L)
{}
/×
friend bool operator > ( const Runner& lhs, const Runner& rhs )
{
return lhs.position > rhs.position;
}×/
};
bool need_change_tactic( const Runner& runner, const Accuracy_Control& ac )
{
return runner.status_remain_time < ac.time_acc;
}
template< typename Run_Tactic, typename Rest_Tactic >
void change_tactic( Runner& runner, const Run_Tactic& run_tactic,
const Rest_Tactic& rest_tactic, const Accuracy_Control& ac )
{
if ( runner.is_running )
rest_tactic( runner);
else
run_tactic( runner, ac );
}
void time_flying( Runner& runner, const Accuracy_Control& ac )
{
runner.position += runner.speed;
runner.status_remain_time -= ac.time_acc;
runner.time += ac.time_acc;
}
void correct_position( Runner& runner, const Racetrack& racetrack )
{
if ( runner.position > racetrack.circumference )
{
runner.position -= racetrack.circumference;
//last_relation = !last_relation;
}
}
bool do_they_meet( const Runner& runner_a, const Runner& runner_b, const Accuracy_Control& ac )
//bool do_they_meet( const Runner& runner_a, const Runner& runner_b, const Accuracy_Control& )
{
/*
static bool last_relation = true;
bool ans = false;
if ( ( runner_a > runner_b ) != last_relation )
ans = true;
last_relation = runner_a > runner_b;
return ans;
*/
bool ans = false;
long double difference = runner_a.position - runner_b.position;
if ( difference < 0 )
difference = -difference;
if ( difference < ac.position_acc )
ans = true;
return ans;
}
struct Run_Tactic_A
{
void operator()( Runner& runner, const Accuracy_Control& ac ) const
{
runner.is_running = true;
runner.speed = 52.0L / 60.0L * ac.time_acc;
runner.status_remain_time = 100.0L / 52.0L;
}
};
struct Run_Tactic_B
{
void operator()( Runner& runner, const Accuracy_Control& ac ) const
{
runner.is_running = true;
runner.speed = 46.0L / 60.0L * ac.time_acc;
runner.status_remain_time = 100.0L/ 46.0L;
}
};
struct Rest_Tactic
{
void operator()( Runner& runner ) const
{
runner.is_running = false;
runner.speed = 0.0L;
runner.status_remain_time = 60.0L;
}
};
#include <iostream>
using namespace std;
int main()
{
Accuracy_Control ac;
Racetrack racetrack;
Runner runner_a, runner_b;
for ( long long l = 0; l < 99999999; ++l )
{
if ( need_change_tactic( runner_a, ac ) )
{
change_tactic( runner_a, Run_Tactic_A(), Rest_Tactic(), ac );
}
if ( need_change_tactic( runner_b, ac ) )
{
change_tactic( runner_b, Run_Tactic_B(), Rest_Tactic(), ac );
}
time_flying( runner_a, ac );
time_flying( runner_b, ac );
correct_position( runner_a, racetrack );
correct_position( runner_b, racetrack );
if ( do_they_meet( runner_a, runner_b, ac ) )
{
cout << "\nA. Find a meet at the time " << runner_a.time
<< " at the position " << runner_a.position;
cout << "\nB. Find a meet at the time " << runner_b.time
<< " at the position " << runner_b.position;
}
}
return 0;
}
假设甲用t分钟追上乙 此时甲行驶了(x百米+y米) 其中y<100
则甲实际跑了 (t-x) 分钟 总路程为 100*x+y
乙实际跑了 [t-(x-4)] 分钟 总路程为 100*(x-4)+ y
从而可以列出下面两个等式 :
100*x+y = 52*(t-x)
100*(x-4)+y = 46*[t-(x-4)]
从中可以求得 t-x = 292/3 (甲实际跑的时间)
甲实际跑的路程为 292/3 * 52 = 15184/3 (整数部分为5061)
所以甲中途休息了 50分钟
总的时间为 292/3 + 50 = 147+ 1/3