zoukankan      html  css  js  c++  java
  • UVa1586 Molar mass

    #include <stdio.h>

    int GetQuantity(char* q, char** p)
    {
        int quantity = 0;
        while (*q && '0' <= *q && *q <= '9')
        {
            quantity = quantity*10 + (*q-'0');
            ++q;
        }
        if (quantity == 0)
            quantity = 1;
        *p = q;
        return quantity;
    }

    int main()
    {
        int T, quantity;
        double mass;
        char str[81], *p;
        scanf("%d", &T);
        while (T--)
        {
            scanf("%s", str);
            mass = 0.0;
            p = str;
            while (*p)
            {
                if (*p == 'C')
                {
                    quantity = GetQuantity(p+1, &p);
                    mass += (12.01*quantity);
                }
                else if (*p == 'H')
                {
                    quantity = GetQuantity(p+1, &p);
                    mass += (1.008*quantity);
                }
                else if (*p == 'O')
                {
                    quantity = GetQuantity(p+1, &p);
                    mass += (16.00*quantity);
                }
                else if (*p == 'N')
                {
                    quantity = GetQuantity(p+1, &p);
                    mass += (14.01*quantity);
                }
                else
                {
                    ++p;
                }
            }
            
            printf("%.3f ", mass);
        }

        return 0;
    }

  • 相关阅读:
    关内存地址的分配
    关于URL
    linux的8小时差问题解决
    关于Scanner类
    域名后缀
    匿名对象用法
    final修饰符,多态,抽象类,接口
    二维数组的传参
    关于随机数
    面向对象编程的三大基本特征
  • 原文地址:https://www.cnblogs.com/danny1221/p/4591363.html
Copyright © 2011-2022 走看看