UVa-1586 Molar mass

#include<iostream>

#include<cstdio>

#include<cstring>

#include<ctype.h>

using namespace std;

double change(char a)

{

    if(a=='C') return 12.010;

    else if(a=='H') return 1.008;

    else if(a=='O') return 16.000;

    else if(a=='N') return 14.010;

}

bool is_digit(char a)

{

    if(a>='0'&&a<='9') return 1;

    else return 0;

}

int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        char a[85]={};

        cin>>a;

        int k=strlen(a);

        double sum=0.000;

        for(int i=0;i<k;i++)

        {

            if(isalpha(a[i]))

            {

                if(is_digit(a[i+1]))

                {

                    if(is_digit(a[i+2]))

                        sum+=((a[i+1]-'0')*10+(a[i+2]-'0'))*change(a[i]);

                    else

                        sum+=(a[i+1]-'0')*change(a[i]);

                }

                else sum+=change(a[i]);

            }

        }

        printf("%.3f\n",sum);

    }

}

你可能感兴趣的:(uva)