TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

MixtureTemp

Problem statement, definition, constraints, and public examples.

Problem Statement

When two immiscible liquids (two liquids that don't blend together, like oil and water) are put together, the final temperature of the mixture is determined by the initial temperatures of each of the liquids, the number of grams of each of the liquids, and the specific heat of each of the liquids. The specific heat of a liquid is the amount of energy required to raise the temperature of one gram of the liquid by 1 degree Celsius. This is equal to the amount of energy that is taken out of 1 gram of the liquid when its temperature is lowered by 1 degree Celsius. So, when one liquid is hotter than another, its temperature drops, while the other liquid's temperature increases, until the total amount of energy gained by the lower temperature liquid is equal to the total amount lost by the higher temperature one and the tempuratures of the two liquids are equal. For example, if you were mixing 10 grams of liquid A, with 10 grams of liquid B, and A had a specific heat of 1, while B had a specific heat of 2, and A started at 100 degrees, while B started at 103 degrees, then the final temperature would be 102 degrees. Raising the temperature of A by 2 degrees requires 2 units of energy per gram, for a total of 20 units of energy. Similarly, lowering the temperature of B by 1 degree gives off 2 units of energy per gram, for a total of 20 units of energy. You will be given two int[]s, A and B, representing the two liquids. Each of these will have three elements:
  • Element 0: Specific Heat
  • Element 1: Temperature
  • Element 2: Number of Grams
Your method should return the final temperature of the mixture.

Definition

Class:
MixtureTemp
Method:
finalTemp
Parameters:
int[], int[]
Returns:
double
Method signature:
double finalTemp(int[] A, int[] B)
(be sure your method is public)

Notes

  • As long as your return has absolute or relative error of less then 1e-9, it will be judged correct.

Constraints

  • Both A and B will contain exactly three elements.
  • Each element of A and B will be between 1 and 1000, inclusive.

Examples

  1. {1,100,10}
    {2,103,10}
    Returns: 102.0
    The example from the problem statement.
  2. {1,100,10}
    {2,103,20}
    Returns: 102.4
    These are the same two liquids as in the first example, but there is now twice as much of liquid B. At 102.4 degrees, liquid A has gained 2.4 degrees, for 2.4*1*10 = 24 total units of energy. Liquid B has lost .6 degrees, for .6*2*20 = 24 units of energy. Thus, at 102.4, liquid A has gained the same amount of energy that liquid B has lost.
  3. {134,1000,104}
    {212,103,32}
    Returns: 706.3104247104247
  4. {100,100,1}
    {20,100,34}
    Returns: 100.0
Back to all problems