USA1586 Molar mass

来源:互联网 发布:大卫罗宾逊数据 编辑:程序博客网 时间:2024/05/22 17:18

原题:



代码:

<span style="font-size:18px;">#include <iostream>#include <cstdio>#include <cctype>#define maxn 80char chem[maxn];int main() {    using namespace std;    int T, n;    int i;    double mass, per;    char c;    if (scanf("%d",&T)==1) {        while (T--) {            scanf("%s", chem);            mass= 0;            for ( i=0; chem[i]!='\0'; i++) {                c= chem[i];                n= 1;                if(c=='C')  per= 12.01;                else if (c=='N')  per= 14.01;                else if (c=='O')  per= 16.00;                else  per= 1.008;  // c=='H'                if (isdigit(chem[i+1])) {                    i++;                    n= chem[i]-'0';                    while (isdigit(chem[i+1])) {                        i++;                        n= n*10 + chem[i] - '0';                    }                    mass+= n*per;                }                else                    mass+= n*per;            }            printf("%.3f\n", mass);        }    }    return 0;}</span><span style="font-size:24px;"></span>


0 0
原创粉丝点击