Martin’s father goes for a jog every morning. Martin follows him several minutes later. His father starts at a position that is X1 meters away from their home and runs rectilinearly at a constant speed of V1 meters per step for N steps.
Martin is standing at X2 meters away from his home. He wonders how fast he must run at some constant speed of V2 meters per step so as to maximize F, where F equals the number of his father's footsteps that Martin will land on during his run. It is given that the first step that Martin will land on, from his starting position, will have been landed on by his father. Note that if more than one prospective velocity results in the same number of maximum common steps, output the highest prospective velocity as V2.
Write an program to help Martin calculate F and V2.
Answers
Answer:
int[] solve(int x1, int x2, int v1, int n){
/*
result[0]: common steps;
result[1]: v2;
*/
int[] result = new int[2];
int[] steps = new int[n+1];
for (int i=0; i<=n; i++)
steps[i] = x1 + v1*i - x2;
for(int i=0; i<=n; i++){
if(steps[i]<=0) continue;
int v2 = steps[i];
int count = 0;
for(int j=i; j<=n; j++){
if(steps[j]%v2 == 0)
count++;
}
if(result[0]<=count){
result[0] = count;
result[1] = v2;
}
}
return result;
}
Answer:
def function(fatherPos,martinPos,velFather,steps):
result = [0]*2
steps1 = [0]*steps
for i in range(0,steps):
steps1[i] = fatherPos + velFather*i - martinPos;
for i in range(0,steps):
if(steps1[i]<=0):
continue
v2 = steps1[i]
count = 0
for j in range(i,steps):
if(steps1[j]%v2 == 0):
count+=1
if(result[0]<=count):
result[0] = count
result[1] = v2
return result
fatherPos = int(input())
martinPos = int(input())
velFather = int(input())
steps = int(input())
result = function(fatherPos,martinPos,velFather,steps)
print(result)
Explanation:
\