UVA1586 UVALive3900 Molar mass【输入输出+水题】

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

CC6H5OH

NH2CH2COOH

C12H22O11

Sample Output

12.010

94.108

75.070

342.296


Regionals 2007 >> Asia - Seoul


问题链接:UVA1586 UVALive3900 Molar mass。

问题简述:(略)

问题分析

  这个问题是根据分子式,求分子量。

程序说明

  原子量使用map表来存储,所以用C++来编程。

  程序中,使用函数getchar()处理输入流,需要更高的编程技巧。


AC的C++语言程序如下:

/* UVA1586 UVALive3900 Molar mass */

#include 
#include 
#include 
#include 

using namespace std;

map aweight;

int main()
{
    int t, count, mflag;
    char c, molar;
    double molarmass;

    aweight['C'] = 12.01;
    aweight['H'] = 1.008;
    aweight['O'] = 16.00;
    aweight['N'] = 14.01;

    cin >> t;
    getchar();
    while(t--) {
        molarmass = 0.0;

        mflag = 0;
        count = 0;
        while((c=getchar()) != '\n' && c != EOF) {
            if(isalpha(c)) {
                if(mflag)
                    molarmass += ((count==0) ? 1 : count) * aweight[molar];
                molar = c;
                count = 0;
                mflag = 1;
            } else if(isdigit(c))
                count = count * 10 + c - '0';
        }
        if(mflag)
            molarmass += ((count==0) ? 1 : count) * aweight[molar];


        // 输出结果
        if(mflag)
            printf("%.3f\n", molarmass);
    }

    return 0;
}


你可能感兴趣的:(#,ICPC-备用二,#,ICPC-打表与查表,#,ICPC-UVALive,#,ICPC-UVA,#,ICPC-水题题解三)