5139. Average train speed

 

Find the average speed of the train, if it traveled the first half of the distance at a speed of v1​, and the second half at a speed of v2.

 

Input. Two integers v1 and v2 (1 ≤ v1, v2 ≤ 109).

 

Output. Print the average speed of the train with 4 decimal places.

 

Sample input 1

Sample output 1

10 10

10.0000

 

 

Sample input 2

Sample output 2

50 100

66.6667

 

 

SOLUTION

mathematics

 

Algorithm analysis

Let s be the distance traveled by the train.

The train will cover the first half of the distance in time  = .

The train will cover the second half of the distance in time  = .

The time it takes for the train to cover the entire distance is equal to

s / (2v1) +  s / (2v2)

Then the average speed of the train equals the length of the path divided by the time taken to cover that path, which is

vaverage =  = =

 

Algorithm realization

Read the input data.

 

scanf("%lf %lf",&v1,&v2);

 

Compute and print the average speed.

 

ave = 2 * v1 * v2 / (v1 + v2);

printf("%.4lf\n",ave);