Connection Status:
Competition Arena > MixtureDensity
SRM 375 · 2007-11-10 · by darnley · Simple Math, String Manipulation
Class Name: MixtureDensity
Return Type: double
Method Name: getDensity
Arg Types: (vector<string>)
Problem Statement

Problem Statement

A pharmacist is making a mixture of several ingredients. She needs to know the density of the resulting mixture. Density is defined as the mass of the mixture divided by its volume.

You're given a String[] ingredients, each element of which describes a single ingredient and is formatted "<Volume> ml of <Name>, weighing <Mass> g" (quotes for clarity). <Volume> and <Mass> are integers representing the volume in milliliters and mass in grams, respectively. <Name> is the name of the ingredient.

Return the density of the resulting mixture in grams per milliliters.

Notes

  • The returned value must be accurate to within a relative or absolute value of 1E-9.

Constraints

  • ingredients will contain between 1 and 50 elements, inclusive.
  • Each element of ingredients will contain between 23 and 50 characters, inclusive.
  • Each element of ingredients will be formatted as described in the statement.
  • Each <Volume> in ingredients will be an integer between 1 and 1000, inclusive, with no leading zeroes.
  • Each <Mass> in ingredients will be an integer between 1 and 1000, inclusive, with no leading zeroes.
  • Each <Name> in ingredients will contain only lowercase letters ('a'-'z') and spaces (' ').
  • Each <Name> in ingredients will begin and end with a lowercase letter.
Examples
0)
{"200 ml of oil, weighing 180 g"}
Returns: 0.9

The density is mass/volume = 180g/200ml = 0.9g/ml, the answer is 0.9.

1)
{"100 ml of dichloromethane, weighing 130 g", "100 ml of sea water, weighing 103 g"}
Returns: 1.165

The volume of the resulting mixture is 200 ml and the mass is 233 g. Therefore, the density is 233g/200ml = 1.165g/ml.

2)
{"500 ml of beer, weighing 525 g", "100 ml of vodka, weighing 92 g"}
Returns: 1.0283333333333333
3)
{"1000 ml of water, weighing 1000 g", "500 ml of orange juice concentrate, weighing 566 g"}
Returns: 1.044
4)
{"1000 ml of something   l i g h t, weighing 1 g"}
Returns: 0.0010

Submissions are judged against all 48 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MixtureDensity with a public method double getDensity(vector<string> ingredients) · 48 test cases · 2 s / 256 MB per case

Submitting as anonymous