Reverse Gear
Problem Description:
A futuristic company is building an autonomous car. The scientists at the company are training the car to perform Reverse parking. To park, the car needs to be able to move in backward as well as forward direction. The car is programmed to move backwards B meters and forwards again, say F meters, in a straight line. The car does this repeatedly until it is able to park or collides with other objects. The car covers 1 meter in T units of time. There is a wall after distance D from car's initial position in the backward direction.
The car is currently not without defects and hence often hits the wall. The scientists are devising a strategy to prevent this from happening. Your task is to help the scientists by providing them with exact information on amount of time available before the car hits the wall.
The car is currently not without defects and hence often hits the wall. The scientists are devising a strategy to prevent this from happening. Your task is to help the scientists by providing them with exact information on amount of time available before the car hits the wall.
Input Format:
First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 4 values delimited by space
F B T D, where
First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 4 values delimited by space
F B T D, where
- F denotes forward displacement in meters
- B denotes backward displacement in meters
- T denotes time taken to cover 1 meter
- D denotes distance from Car's starting position and the wall in backward direction
Output Format:
For each test case print time taken by the Car to hit the wall
For each test case print time taken by the Car to hit the wall
Constraints:
First move will always be in backward direction
1 <= N <= 100
backward displacement > forward displacement i.e. (B > F)
forward displacement (F) > 0
backward displacement (B) > 0
time (T) > 0
distance (D) > 0
All input values must be positive integers only
SNo. | Input | Output |
---|---|---|
1 | 2 6 9 3 18 3 7 5 20 | 162 220 |
Program:
#include <stdio.h>
int main() {
int f,b,d,t,sdist,sdisp,step,rd,td,tot,tt,test;
scanf("%d",&test);
while(test)
{
scanf("%d %d %d %d",&f,&b,&t,&d);
sdist=b+f;
sdisp=b-f;
step=(d-b)/sdisp;
if((d-b)%sdisp!=0)
step=step+1;
rd=d-(step*sdisp);
tot=(step*sdist)+rd;
tt=tot*t;
printf("%d\n",tt);
test--;
}
return 0;
}
Output:
2
6 9 3 18
3 7 5 20
162
220
You can also run it on an online IDE: https://ide.geeksforgeeks.org/S994F0QfMs
Your feedback and if you have any doubts it is welcomed! Do contact me or comment it below! Cheers!
Related Links: consecutive prime sum
6 9 3 18
3 7 5 20
162
220
You can also run it on an online IDE: https://ide.geeksforgeeks.org/S994F0QfMs
Your feedback and if you have any doubts it is welcomed! Do contact me or comment it below! Cheers!
Related Links: consecutive prime sum
Bro can you please explain a bit of your logic and how you approached the problem, I can't completely understand it.
ReplyDelete