UVA1586 Molar Mess

来源:互联网 发布:全炫茂 知乎 编辑:程序博客网 时间:2024/06/05 17:52

Anorganiccompound  isanymemberofalargeclassofchem-

ical compounds whose molecules contain carbon. The  molar

mass ofanorganiccompoundisthemassofonemoleofthe

organiccompound. Themolarmassofanorganiccompound

can be computed from the standard atomic weights of the

elements.

When an organic compound is given as a molecular for-

mula,Dr. CHONwantstofinditsmolarmass.     Amolecular

formula,suchasC  H O ,identifieseachconstituentelementby

3

4 3

itschemicalsymbolandindicatesthenumberofatomsofeach

elementfoundineachdiscretemoleculeofthatcompound.     If

a molecule contains more than one atom of a particular ele-

ment,thisquantityisindicatedusingasubscriptafterthechemicalsymbol.

In this problem, we assume that the molecular formula is represented by only four elements, ‘C’

(Carbon),‘H’(Hydrogen),‘O’(Oxygen),and‘N’(Nitrogen)withoutparentheses.

Thefollowingtableshowsthatthestandardatomicweightsfor‘C’,‘H’,‘O’,and‘N’.

AtomicName

StandardAtomicWeight   12.01g/mol   1.008g/mol  16.00g/mol   14.01g/mol

Carbon

Hydrogen

Oxygen

Nitrogen

Forexample,themolarmassofamolecularformulaC      H OHis94.108g/molwhichiscomputedby

6

5

6×(12.01g/mol)+6×(1.008g/mol)+1×(16.00g/mol).

Givenamolecularformula,writeaprogramtocomputethemolarmassoftheformula.

Input

Yourprogramistoreadfromstandardinput.     TheinputconsistsofT   testcases. Thenumberoftest

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

chemicalsymbolwouldbeomittedwhenthenumberis1(2≤n≤99).


Output

Yourprogramistowritetostandardoutput.    Printexactlyonelineforeachtestcase.     Thelineshould

containthemolarmassofthegivenmolecularformula.

Sample Input

4

C

C6H5OH

NH2CH2COOH

C12H22O11

Sample Output

12.010

94.108

75.070

342.296 

简单的STL,用栈能够简单的解决,当然不用栈也行

//用了栈#include<iostream>#include<cstdio>#include<stack>#include<cstring>#define maxn 1000using namespace std;int main(){    int x;    char s[maxn];    scanf("%d",&x);    while(x--)    {        scanf("%s",s);        stack<float> st;        int len=strlen(s),sum;        float k;        for(int i=0; i<len; i++)        {            if(s[i]=='C')            {                k=12.01;                st.push(k);            }            else if(s[i]=='H')            {                k=1.008;                st.push(k);            }            else if(s[i]=='O')            {                k=16.00;                st.push(k);            }            else if(s[i]=='N')            {                k=14.01;                st.push(k);            }            if(isdigit(s[i])&&(isalpha(s[i-1])))            {                sum=s[i]-'0';                st.top()*=sum;            }            if((i>=1)&&isdigit(s[i])&&(isdigit(s[i-1])))            {                sum=s[i]-'0';                st.top()*=10.0;                st.top()+=(k*sum);            }        }        int l=st.size();        float sa=0.0;        for(int i=0; i<l; i++)        {            sa+=st.top();            st.pop();        }        printf("%.3f\n",sa);    }    return 0;}
下边是模拟的栈

#include<stdio.h>#include<string.h>#include<ctype.h>#define maxn 1000float vec[maxn];int fl=0;int push(float k){    fl++;    vec[fl]=k;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(vec,0,sizeof(vec));        fl=0;        char s[maxn];        scanf("%s",s);        int i,l=strlen(s);        float k,sum=0.0;        for(i=0; i<l; i++)        {            if(s[i]=='C')            {                k=12.01;                push(k);            }            else if(s[i]=='H')            {                k=1.008;                push(k);            }            else if(s[i]=='O')            {                k=16.00;                push(k);            }            else if(s[i]=='N')            {                k=14.01;                push(k);            }            if(isdigit(s[i]) && isalpha(s[i-1]))            {                int tem=s[i]-'0';                vec[fl]*=tem;            }            if((i>=1) && isdigit(s[i]) && isdigit(s[i-1]) )            {                int tem=s[i]-'0';                vec[fl]*=10;                vec[fl]+=(k*tem);            }        }        for(i=1; i<=fl; i++)        {            sum+=vec[i];        }        printf("%.3f\n",sum);    }    return 0;}



0 0
原创粉丝点击