您的位置:首页 > 其它

UVa-1586

2017-02-21 21:49 393 查看
An organic compound is any member of a large class of chemicalcompounds whose molecules contain carbon. The molarmass of an organic compound is the mass of one mole of theorganic compound. The molar mass of an organic compoundcan
be computed from the standard atomic weights of theelements.

When an organic compound is given as a molecular formula,Dr. CHON wants to find its molar mass. A molecularformula, such as C3H4O3, identifies each constituent element byits chemical symbol
and indicates the number of atoms of eachelement found in each discrete molecule of that compound. Ifa molecule contains more than one atom of a particular element,this quantity is indicated using a subscript after the chemical symbol.In this problem, we assume
that the molecular formula is represented by only four elements, ‘C’(Carbon), ‘H’ (Hydrogen), ‘O’ (Oxygen), and ‘N’ (Nitrogen) without parentheses.The following table shows that the standard atomic weights for ‘C’, ‘H’, ‘O’, and ‘N’.



For example, the molar mass of a molecular formula C6H5OH is 94.108 g/mol which is computed by6 × (12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol).Given a molecular formula, write a program to compute the molar mass of the
formula.

Input

Your program is to read from standard input. The input consists of T test cases. The number of testcases T is given in the first line of the input. Each test case is given in a single line, which containsa molecular formula as
a string. The chemical symbol is given by a capital letter and the length ofthe string is greater than 0 and less than 80. The quantity number n which is represented after thechemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).

Output

Your program is to write to standard output. Print exactly one line for each test case. The line shouldcontain the molar mass of the given molecular formula.

Sample Input

4

C

C6H5OH

NH2CH2COOH

C12H22O11

Sample Output

12.010

94.108

75.070

342.296

欢迎交流:

#include<stdio.h>
#include<string.h>
char s[85];
int main()
{
int t;
scanf("%d",&t);
int i = 0;
double before = 0.0;
while (t--)
{
double sum = 0.0;
scanf("%s",s);
int len = strlen(s);
for (int i = 0; i < len; i++)
{
if (s[i] == 'C')
{
sum += 12.01; before = 12.01;
}
else if (s[i] == 'H')
{
sum += 1.008;  before = 1.008;
}
else if (s[i] == 'O')
{
sum += 16.00;  before = 16.00;
}
else if (s[i] == 'N')
{ sum += 14.01;  before = 14.01; }
else
{
int x = 0;
if ((s[i + 1] - '0' <= 9) && (s[i + 1] - '0' >= 0))
{
x = (s[i] - '0') * 10 + (s[i + 1] - '0');
i++;
}
else x = s[i]-'0';
sum += (x-1)* before;//之前加了一次,减1
}
}
printf("%.3lf\n",sum);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: