#P1994. Combustion Coefficients of an Organic Compound
Combustion Coefficients of an Organic Compound
Combustion Coefficients of an Organic Compound
Given a molecular formula of an organic compound, determine the coefficients of CO2 and H2O in its complete combustion reaction with oxygen.
The combustion of an organic compound of the form CnHm[Op] (where the oxygen part is optional) follows the reaction:
$$C_nH_m[O_p] + \left(n+\frac{m}{4}-\frac{p}{2}\right) O_2 \to n CO_2 + \frac{m}{2} H_2O $$Since the oxygen atoms in the organic compound do not affect the coefficients of CO2 and H2O, you only need to output:
- The coefficient of CO2 which is n (the number of carbon atoms).
- The coefficient of H2O which is m/2 (half the number of hydrogen atoms).
Note: The input is guaranteed to be a valid molecular formula with elements in the order C, H, and optionally O. If an element appears without a number, it implies a count of 1. You may assume that the number of hydrogen atoms is even so that m/2 is an integer.
inputFormat
A string representing the molecular formula of an organic compound (e.g., "C2H6", "CH4", "C3H8", "CH2O", "C4H10").
outputFormat
Output two integers separated by a space. The first integer is the coefficient of CO2 and the second is the coefficient of H2O in the combustion reaction.
sample
CH4
1 2