UVa 1586 Molar mass 分子量 题解

英文

Description

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standard atomic weights of the elements.
这里写图片描述
When an organic compound is given as a molecular formula, Dr. CHON wants to find its molar mass. A molecular formula, such as C3H4O3, identifies each constituent element by its chemical symbol and indicates the number of atoms of each element found in each discrete molecule of that compound. If a 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’.
这里写图片描述

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case is given in a single line, which contains a molecular formula as a string. The chemical symbol is given by a capital letter and the length of the string is greater than 0 and less than 80. The quantity number n which is represented after the chemical 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 should contain 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

中文

题目大意:

给出一种物质的分子式(不带括号),求分子量。本题中的分子式只包含4种原子,分别为C,H,O,N,原子质量分别为12.01,1.008,16.00,14.01(单位g/mol)。例如,C6H5OH的分子量为94.108g/mol。

提示:

唯一要注意的就是细节,这道题的细节坑死人啊,原以为一道水题,调了好长时间。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define mC 12.01
#define mH 1.008
#define mO 16.00
#define mN 14.01
using namespace std;
char s[5001];
int T,t=0,p[10],ls;
int f1(char k)
{
    if(k=='C') return 1;
    if(k=='H') return 2;
    if(k=='O') return 3;
    if(k=='N') return 4;
}
int main()
{
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        ls=0;
        memset(p,0,sizeof(p));
        scanf("%s",s);
        t=f1(s[0]);
        for(int j=1;j<strlen(s);j++)
        {
            if(s[j]=='C'||s[j]=='H'||s[j]=='O'||s[j]=='N')
            {
                p[t]+=ls;
                ls=0;
                if(s[j-1]<'0'||s[j-1]>'9') p[t]++;
                t=f1(s[j]);
            } else 
            {
                if(s[j-1]>='0'&&s[j-1]<='9') ls=ls*10+int(s[j])-48;
                else ls+=int(s[j])-48;
            }
        }
        p[t]+=ls;
        if(s[strlen(s)-1]<'0'||s[strlen(s)-1]>'9') p[f1(s[strlen(s)-1])]++;
        double ans=p[1]*mC+p[2]*mH+p[3]*mO+p[4]*mN;
        printf("%.3lf\n",ans);
    }
    return 0;
}

相关链接:

UVa题解小全:
https://blog.csdn.net/zj_mrz/article/details/81144019

UVa 1583 Digit Generator 生成元 题解:
https://blog.csdn.net/zj_mrz/article/details/81143855

UVa 1585 Score 得分 题解:
https://blog.csdn.net/zj_mrz/article/details/81144159

猜你喜欢

转载自blog.csdn.net/zj_mrz/article/details/81165988