Molar Mass, ACM/ICPC Seoul 2007,Uva1586 给出一种物质的分子式,求分子量

/*该代码没有验证是否可以AC,但是原理即是如此,
注意一下格式,或者题目的输入输入条件*/

#include 
#include 
#define maxn 100
int main()
{
    char s[maxn];
    memset(s, '0', sizeof(s)); 
    while(scanf("%s", s) == 1)
    {
        double sum = 0.0;
        for(int i = 0; i < strlen(s); i++)
        {
            switch(s[i])
            {
                case 'C':
                    {
                        if(s[i+1] > '0' && s[i+1] <= '9')
                            sum += (s[i+1] - '0') * 12.01;
                        else
                            sum += 12.01;
                        break; 
                    }
                case 'H':
                    {
                        if(s[i+1] > '0' && s[i+1] <= '9')
                            sum += (s[i+1] - '0') * 1.008;
                        else
                            sum += 1.008;
                        break;
                    }
                case 'O':
                    {
                        if(s[i+1] > '0' && s[i+1] <= '9')
                            sum += (s[i+1] - '0') * 16.00;
                        else
                            sum += 16.00;
                        break;
                    }
                case 'N':
                    {
                        if(s[i+1] > '0' && s[i+1] <= '9')
                            sum += (s[i+1] - '0') * 14.01;
                        else
                            sum += 14.01;
                        break;
                    }
                default:
                    break;
            }
        }
        printf("%.3fg/mol", sum);
    }

    return 0;
 } 

你可能感兴趣的:(uva)