uva 1586 Molar mass

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int change(char ch)
{
    return ch-'1';
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        double m=0;
        double sum=0;
        double c[100],d[100];
        memset(c,0,sizeof(c));
        for(int i=0;i<(int)a.length();i++)
            d[i]=1;
        if((int)a.length()==1)
        {
            if(a[0]=='C') {sum=12.010;m=0;}
            if(a[0]=='H') {sum=1.008;m=0;}
            if(a[0]=='O') {sum=16.000;m=0;}
            if(a[0]=='N') {sum=14.010;m=0;}
        }
        else
        {
            for(int i=0;i<(int)a.length();i++)
            {
                if(a[i]=='C') {c[i]=12.010;m=0;}
                else if(a[i]=='H') {c[i]=1.008;m=0;}
                else if(a[i]=='O') {c[i]=16.000;m=0;}
                else if(a[i]=='N') {c[i]=14.010;m=0;}
                else
                {
                    if(m==0) {d[i-1]+=change(a[i]);m=1;}
                    else{d[i-2]=d[i-2]*10+(a[i]-'0');}
                }
            }
        }
        for(int i=0;i<(int)a.length();i++)
        {
            if(c[i]) sum+=c[i]*d[i];
        }
        printf("%.3f\n",sum);
    }
    return 0;
}

你可能感兴趣的:(uva 1586 Molar mass)