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.
Suppose the inputs are:
Acceleration A = 2 \, \text{m/s}^2
Initial velocity V = 1 \, \text{m/s}
Target time T = 1 \, \text{s}
To solve this, we calculate the time taken to travel each meter until we find the first one completed in under seconds.
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.
To find out how long it takes to traverse each individual meter:
Calculate the time t_{n-1} to reach the position at (N-1) meters.
Calculate the time t_n to reach meters.
The time taken to traverse the -th meter alone is:
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 t_N and t_{N-1} for each meter incrementally.
Check if \Delta t_N is less than . If so, we've found our answer. Otherwise, increment the meter and continue.
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++;
}
}