Розбір
Written by al95ireyiz
A rocket is launched with an initial velocity and constant acceleration . Our goal is to find the first meter along the rocket's path that it will traverse in less than a given time .
As the rocket accelerates, each subsequent meter will take less time to travel.
Example
Suppose the inputs are:
Acceleration
Initial velocity
Target time
To solve this, we calculate the time taken to travel each meter until we find the first one completed in under seconds.
Kinematic Equation with Initial Velocity
We use the kinematic equation for motion with constant acceleration:
where:
is the distance traveled.
is the initial velocity.
is the constant acceleration.
is the time taken.
To find the time to reach a given distance , we rearrange the equation as:
This formula provides the time needed to travel a distance from the starting point.
Calculating the Time to Traverse Each Meter
To find out how long it takes to traverse each individual meter:
Calculate the time to reach the position at meters.
Calculate the time to reach meters.
The time taken to traverse the -th meter alone is:
Writing the Program
We implement this step-by-step in C++ as follows:
Set up variables for acceleration, initial velocity, and target time. Initialize a
res
variable to 1, representing the current meter.Calculate and for each meter incrementally.
Check if is less than . If so, we've found our answer. Otherwise, increment the meter and continue.
Full Code Example
int main() { double A, V, T, time=0.0, now, delta; int res = 1; cin >> A >> V >> T; while (true) { // Calculate time to reach the end of the current meter // using formula mentioned before now = (-V + sqrt(V * V + 2 * A * res)) / A; // Time taken to traverse this meter delta = now - time; // Check if time to traverse this meter is less than target time if (delta < T) { cout << res << endl; return 0; } time = now; res++; } }