​   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. css中a元素放长英文字母或者数字自动换行的解决

    在做链接分享页面的时候遇到a元素中的下载链接长英文溢出不换行的问题 在给他以及他父元素设置宽度依然没有解决这个问题 最后解决办法给元素加上word-wrap:break-word 解释:使用break ...

  2. Lambda 表达式简介

    0.预备知识 函数式接口:只包含一个抽象方法的接口. 内部类:静态.成员内部类 局部内部类 匿名内部类 1.代码 1 /** 2 * 函数式编程: 3 * lambda表达式前提: 4 * 必须是函数 ...

  3. 第48天学习打卡(CSS)

    HTML + CSS +JavaScript 结构+表现+交互 HTML:结构 CSS:表现 JavaScript:交互 1什么是CSS 如何学习 ​ 1.CSS是什么 ​ 2.CSS怎么用(快速入门 ...

  4. centos7 SNMP错误记录

    如果本地测试ok,远程测试出现如下报错: No Such Object available on this agent at this OID或No more variables left in th ...

  5. Docker Hub 镜像加速器

    一.概述 国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器.Docker 官方和国内很多云服务商都提供了国内加速器服务. 二.配置加速地址 Ubuntu 16.04+.De ...

  6. 007-变量的作用域和LED点阵

    变量 一.局部变量和全局变量 局部变量:函数内申明的变量,只在函数内有效. 全局变量:函数外部申明的变量.一个源程序文件有一个或者多个函数,全局变量对他们都起作用. 备注:全局变量有副作用,降低了函数 ...

  7. 蓝桥杯-分考场(dfs)

    分考场 PREV-53 这题的解决方法使用dfs,因为数据很小,才100. 每次当前的人人是否可以和前面的组队,设置两个数组group和fri /*DFS求解:思路每次判断输入的人是否可以和前面的组队 ...

  8. 读 Kafka 源码写优雅业务代码:配置类

    这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...

  9. vue 页面生成图片保存

    需求:将页面中的元素转成图片,支持保存或下载.要求下载的图片包含页面背景,头像,用户名,文本为"我的邀请码"和个人二维码. 实现:将页面绘制到canvas中,生成base64图片链 ...

  10. sitemesh简单介绍

    SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的. Sitemesh是由一个基于Web页面布局.装饰以及与现存Web应用整合的框架. 它能帮 ...