Cake#112

Cake (Editorial)

Algorithm Analysis

The productivity of the first chubby will be 1 / t_1 cake per hour. Similarly, the productivity of the second and third chubbies in eating cake are 1 / t_2 and 1 / t_3 cake per hour. If the chubbies eat the cake simultaneously, then in one hour they will eat 1 / t_1 + 1 / t_2 + 1 / t_3 portion of the cake. Therefore, the entire cake can be eaten in 1 / (1 / t_1 + 1 / t_2 + 1 / t_3) hours.

Example

Three chubbies will eat the cake in 1 / (1 / 3 + 1 / 3 + 1 / 3) = 1 hour.

Algorithm Implementation

Read the input data. Calculate the answer using the formula and output it.

scanf("%d %d %d", &t1, &t2, &t3);
res = 1.0 / (1.0/t1 + 1.0/t2 + 1.0/t3);
printf("%.2lf\n", res);

Java Implementation

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner con = new Scanner(System.in);
        int t1 = con.nextInt(),
            t2 = con.nextInt(),
            t3 = con.nextInt();
        double res = 1.0 / (1.0/t1 + 1.0/t2 + 1.0/t3);
        System.out.println(res);
        con.close();
    }
}