​   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. 为什么ConcurrentHashMap,HashTable不支持key,value为null?

    ConcurrentHashmap.HashMap和Hashtable都是key-value存储结构,但他们有一个不同点是 ConcurrentHashmap.Hashtable不支持key或者val ...

  2. MYSQL 悲观锁和乐观锁简单介绍及实现

    1:悲观锁 1.1 特点: 每次查询都会进行锁行,怕"其他人"进行数据的修改. 1.2 实现步骤: 步骤1:开启事务test1,并对id=2的记录进行查询,并加锁,如:   步骤2 ...

  3. 后端程序员之路 41、BlockingQueue

    BlockingQueue,阻塞队列,常用于实现生产者和消费者模型特点:1.队列为空时,取操作会等到队列有数据2.队列满时,存操作会等到队列可用 基于C++11的阻塞队列简单实现 - Cynric 的 ...

  4. 在Arch上使用Fcitx5

    目录 卸载Fcitx4 安装Fcitx5 配置 修改环境变量 系统登陆后默认启动Fcitx5输入法 配置主题 最终使用效果 参考文档 我是一个Arch+KDE的用户,所以下面的方法可能不适合所有的Li ...

  5. MySQL之四 存储引擎

    1.介绍 存储引擎MySQL中的"文件系统" MySQL体系结构 InnoDB存储引擎介绍 My1SAM 和InnoDB区别  mysql MariaDB [(none)]> ...

  6. Kibana 插件环境搭建教程

    原文 环境背景, Kibana 7.4.0, Elasticsearch 7.4.0 注意, 执行以下命令时, 尽量在管理员权限的命令行窗口里执行, 避免一些没有权限的报错; 1. 准备 Kibana ...

  7. Python——input与raw_input的区别

      区别一: raw_input():python2版本 input():python3版本 区别二: raw_input()不管是输数字还是字符串,结果都会以字符串的形式展现出来 input()则是 ...

  8. 冗余网络构建方案对比:VRRP协议、多网卡绑定及WN202冗余链路网卡

    在组建网络时为网络设计冗余方案已经成为提高网络可用性必不可少的一环,伴随着网络技术的发展实现网络冗余的技术方案也是层出不穷,例如应用于服务器端的HA.LB,应用于存储的SAN.DAS.NAS等.本文重 ...

  9. allure报告详解+jenkins配置

    今天的博客分为两部分 1.allure报告实战 2.allure结合jenkins 一.allure 1.allure安装 a.下载路径 https://repo.maven.apache.org/m ...

  10. Apache配置 3.域名跳转

    (1)介绍 当我们变更网站域名或者申多个域名指向一个网站的时候,这个时候我们就会用到域名跳转. (2)配 设置不是以111.com开头的网站都跳转到111.com上. 置 配置 设置不是以111.co ...