​   An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standard atomic weights of the elements.

​   When an organic compound is given as a molecular formula, Dr. CHON wants to find its molar mass. A molecular formula, such as C3H4O3, identifies each constituent element by its chemical symbol and indicates the number of atoms of each element found in each discrete molecule of that compound. If a molecule contains more than one atom of a particular element, this quantity is indicated using a subscript after the chemical symbol.

​   In this problem, we assume that the molecular formula is represented by only four elements, ‘C’ (Carbon), ‘H’ (Hydrogen), ‘O’ (Oxygen), and ‘N’ (Nitrogen) without parentheses.

​   The following table shows that the standard atomic weights for ‘C’, ‘H’, ‘O’, and ‘N’.

Atomic Name Carbon Hydrogen Oxygen Nitrogen
Standard Atomic Weight 12.01 g/mol 1.008 g/mol 16.00 g/mol 14.01 g/mol

​   For example, the molar mass of a molecular formula C6H5OH is 94.108 g/mol which is computed by 6 × (12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol).

​   Given a molecular formula, write a program to compute the molar mass of the formula.

Input

​   Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case is given in a single line, which contains a molecular formula as a string. The chemical symbol is given by a capital letter and the length of the string is greater than 0 and less than 80. The quantity number n which is represented after the chemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).

Output

​   Your program is to write to standard output. Print exactly one line for each test case. The line should contain the molar mass of the given molecular formula.

Sample Input

4
C
C6H5OH
NH2CH2COOH
C12H22O11

Sample Output

12.010
94.108
75.070
342.296

HINT

​   题目大体意思是让你求一个有机分子的摩尔质量,问题的关键是如何求得各个有机分子得各个原子得总数。这里由于每一个原子的总数小于100个,因此可以采用直接判断的方法来求出总数。当然也可以使用递归函数来求出总数,但那就有点不划算了。

Accepted

#include<stdio.h>
#include<string.h> int main()
{
int T;
scanf("%d", &T);
while (T--)
{
char molar[81];
int num[4] = { 0 }; //num数组是用来保存各个原子的总数,下标为0 1 2 3的元素分别是C H O N的总数
scanf("%s", molar);
int len = strlen(molar);
for (int i = 0;i < len;i++)
{
if (molar[i] <= '9' && molar[i] >= '0')continue;
int k;
if (molar[i] == 'C')k = 0;
else if (molar[i] == 'H')k = 1;
else if (molar[i] == 'O')k = 2;
else if (molar[i] == 'N')k = 3; num[k]++; //首先加上一是为了防止元素后面就是元素的情况,如果后面是数字就再后面减去一抵消
if (i + 1 < len && molar[i + 1] <= '9' && molar[i + 1] >= '0')
{
if (i + 2 < len && molar[i + 2] <= '9' && molar[i + 2] >= '0')
num[k] += (molar[i + 1] - '0') * 10 + molar[i + 2] - '0' - 1;
else num[k] += molar[i + 1] - '0' - 1;
}
}
double weigh = num[0] * 12.01 + num[1] * 1.008 + num[2] * 16.00 + num[3] * 14.01;
printf("%.3lf\n", weigh); //求出总质量
} }

Molar mass UVA - 1586的更多相关文章

  1. 【OI】计算分子量 Molar mass UVa 1586 题解

    题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...

  2. UVa 1586 Molar mass --- 水题

    UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...

  3. 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)

    习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...

  4. 分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

    解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同 ...

  5. UVa 1586 Molar mass

    题意:给出物质的分子式,计算它的相对原子质量 因为原子的个数是在2到99之间的,所以找出一个是字母之后,再判断一下它的后两位就可以找出这种原子的个数了 #include<iostream> ...

  6. 分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

    #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf ...

  7. UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言

    关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...

  8. uva 1586 Molar mass(Uva-1586)

    这题做的相当的复杂...之前做的现在应该能简单一点了写的. 我的代码: #include <bits/stdc++.h> using namespace std; main() { int ...

  9. 【习题 3-2 UVA - 1586】Molar mass

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 主要是找那个数字. [代码] #include <bits/stdc++.h> using namespace ...

随机推荐

  1. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  2. WPF -- 构建动画

    写在前面:本文代码摘自<Head First C#> 本文使用ObjectAnimationUsingKeyFrames + Storyboard构建一个动画. ObjectAnimati ...

  3. 第30天学习打卡(异常概述 IO流概述)

    异常概述 即非正常情况,通俗的说,异常就是程序出现的错误 异常的分类(Throwable) 异常(Exception) 合理的应用程序可能需要捕获的问题 举例:NullPointerException ...

  4. C#后端接收前端的各种类型数据

    前端往后端提交数据的方式常用的就这么三种:1.form提交:2.url参数提交:3.json提交 1.针对表单form方式的提交 在后端使用Request.Form的方式接收,比如 前端代码片段: v ...

  5. TorchVision Faster R-CNN 微调,实战 Kaggle 小麦检测

    本文将利用 TorchVision Faster R-CNN 预训练模型,于 Kaggle: 全球小麦检测 上实践迁移学习中的一种常用技术:微调(fine tuning). 本文相关的 Kaggle ...

  6. pytorch(12)ContainersAndAlexNet

    containers graph LR A["Containers"] --> B["nn.Sequetial"] B["nn.Sequetia ...

  7. Java 常见对象 01

    常见对象·Object类 Object类的概述 * A:Object 类概述 * 类层次结构的根类 * 所有类都直接或间接地继承自该类 * B:构造方法 * public Object() * 回想为 ...

  8. 面试常备,字符串三剑客 String、StringBuffer、StringBuilder

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  9. JS(ES6)、Vue.js、node.js

    JS行为(ESMAScript, JSdom, bom)$.ajax() <- (xmlhttpRequest由这个封装来的)  -> axios(vue版)  =  ajax技术jque ...

  10. 漏洞复现-CVE-2018-15473-ssh用户枚举漏洞

          0x00 实验环境 攻击机:Win 10 0x01 影响版本 OpenSSH 7.7前存在一个用户名枚举漏洞,通过该漏洞,攻击者可以判断某个用户名是否存在于目标主机 0x02 漏洞复现 针 ...