Xtreme 10.0 — Counting Molecules
In this article I’m gonna explain about a problem which was given in the IEEEXtreme 10.0 competition which was named as “Counting Molecules”. It’s a real interesting problem to solve. So let’s go into the problem first.
Problem
This is a quite an easy problem to explain. There is a cup of soda which contains distilled water, carbon dioxide and glucose. There is a machine which can count the number of atoms of carbon, hydrogen and oxygen of a given sample. What we have to do is to count the number of molecules of distilled water, carbon dioxide and glucose in that given soda sample given the number of atoms of carbon, hydrogen and oxygen.
Input Format
The input consists of a single line with three space separated integers: c, h, and o.where
c is the count of carbon atoms
h is the count of hydrogen atoms
o is the count of oxygen atoms
Constraints

Output Format
If the number of atoms is consistent with a mixture containing only water, carbon dioxide, and glucose molecules, the output should consist of a single line containing three space separated integers: the number of water molecules, the number of carbon dioxide molecules, and the number of glucose molecules.
If the number of atoms is not consistent with a mixture containing only water, carbon dioxide, and glucose molecules, the output should consist of a line containing the word Error
Sample Input
10 0 20
Sample Output
0 10 0
Explanation
The input indicates that there are 10 carbon atoms and 20 oxygen atoms. The only way that this could occur would be if there were 0 water molecules, 10 carbon dioxide molecules, and 0 glucose molecules.
Now I think it is best to try a solution your own. After that you can come back and compare with my solution.
My Solution
Let’s take the number of carbon, hydrogen and oxygen atoms are c, h and o respectively. Also I will take the number of hydrogen, carbon-dioxide and glucose molecules are x, y and z respectively.
We can get the number of carbon, hydrogen and oxygen atoms in the means of x, y and z. Following are the equations for them.
y + 6z = c
x + 6z = h/2
x + 2y + 6z = o
by solving the above equations the values for x, y and z as,
y = (2 * o-h) / 4
x = (h-2 * c + 2 * y) / 2
z = (c -y) / 6
So we can find the values for x, y and z as we know the c, h and o values. This is quite easy right!!. We are now one step away form the final solution.
According to the problem if the number of atoms is inconsistent we have to output “Error”. That means we can’t have values for x, y and z with fractions. Values we are getting for x, y and z from above equations are decimal values. By simply checking whether the int value is equal to the answer value would do that check ( int(x) != x).
Python Solution on Github
Thanks for reading!!! :)
Comments
Post a Comment