英文

Description

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’.

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

中文

题目大意:

给出一种物质的分子式(不带括号),求分子量。本题中的分子式只包含4种原子,分别为C,H,O,N,原子质量分别为12.01,1.008,16.00,14.01(单位g/mol)。例如,C6H5OH的分子量为94.108g/mol。

提示:

唯一要注意的就是细节,这道题的细节坑死人啊,原以为一道水题,调了好长时间。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define mC 12.01
#define mH 1.008
#define mO 16.00
#define mN 14.01
using namespace std;
char s[5001];
int T,t=0,p[10],ls;
int f1(char k)
{if(k=='C') return 1;if(k=='H') return 2;if(k=='O') return 3;if(k=='N') return 4;
}
int main()
{scanf("%d",&T);for(int i=1;i<=T;i++){ls=0;memset(p,0,sizeof(p));scanf("%s",s);t=f1(s[0]);for(int j=1;j<strlen(s);j++){if(s[j]=='C'||s[j]=='H'||s[j]=='O'||s[j]=='N'){p[t]+=ls;ls=0;if(s[j-1]<'0'||s[j-1]>'9') p[t]++;t=f1(s[j]);} else {if(s[j-1]>='0'&&s[j-1]<='9') ls=ls*10+int(s[j])-48;else ls+=int(s[j])-48;}}p[t]+=ls;if(s[strlen(s)-1]<'0'||s[strlen(s)-1]>'9') p[f1(s[strlen(s)-1])]++;double ans=p[1]*mC+p[2]*mH+p[3]*mO+p[4]*mN;printf("%.3lf\n",ans);}return 0;
}

相关链接:

UVa题解小全:
https://blog.csdn.net/zj_mrz/article/details/81144019

UVa 1225 Digit Counting 题解:
https://blog.csdn.net/zj_mrz/article/details/81207879

UVa 1583 Digit Generator 生成元 题解:
https://blog.csdn.net/zj_mrz/article/details/81143855

UVa 1585 Score 得分 题解:
https://blog.csdn.net/zj_mrz/article/details/81144159

UVa 10935 Throwing cards away I 卡片游戏 题解:
https://blog.csdn.net/zj_mrz/article/details/81208392

转载于:https://www.cnblogs.com/zj-mrz/p/10122460.html

UVa 1586 Molar mass 分子量 题解相关推荐

  1. UVa 1586 -- Molar mass

    一.题目 二.分析 题意要求计算原子总重量. 定义value数组来存放四个原子的数量. value数组大小开到100,应为存放的是大写字母,C,H,O,N,他们的ASCII码大于65. 定义w字符,表 ...

  2. uva 1586 - Molar mass

    本题为小紫书的57页第三题 主要在于判断字母还是数字,若为数字要连续读入整数,如果下一个还是数字 就要i++,如果不是就跳出循环.如果一个是字母下一个也为字母就需要当做把这个字母直接读入一个,要注意的 ...

  3. (UVA)1586 --Molar Mass(分子量)

    题目链接:http://vjudge.net/problem/UVA-1586 思路:统计一个分子式中CHON出现的总次数,乘上相对原子量后求和.要注意的是CH4这样的C后面的1默认不出现,以及C4H ...

  4. 分子量(Molar Mass)数数字(Digit)||UVa 1586,1225

    两个题目均取自UVa,分别为UVa 1586和UVa 1225. 分子量 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4中原子,分别为C, H, O, N,分子量分别为12.01, ...

  5. Molar mass(计算分子量)字符转化

    题目 An organic compound is any member of a large class of chemical compounds whose molecules contain ...

  6. UVA1586 ​​​​​​​ Molar mass

    Molar mass UVA - 1586 题目传送门 题目大意:给你一个只包含C,H,O,N分子式,其中C,H,O,N的原子量分别为:12.01,1.008,16.00,14.01,求其分子量 AC ...

  7. UVa1586 - Molar mass

    //UVa1586 - Molar mass //给出一种由C, H, O, N 四种原子构成的分子式,求分子量 //#define A1 //无法处理换行问题(scanf) //#define A2 ...

  8. 分子量(Molar Mass)

    Description 给出一种物质的得分子式(不带括号), 求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N,原子的量分别为12.01,1.008,16.00,14.01.例如,C6H5 ...

  9. 26行代码AC——习题3-2 分子量 (UVa1586,Molar Mass)——解题报告

    大意: 给出分子式,式中只包含以下四种元素.求分子量. C:12.01 H: 1.008 O: 16.00 N: 14.01 Sample Input 4 C C6H5OH NH2CH2COOH C1 ...

最新文章

  1. 你应该知道的 MongoDB 最佳实践
  2. ZooKeeper 集群:集群概念、选举流程、机器数量
  3. android chrome iframe设置src属性无法启动app
  4. pip 删除安装包_Python中PIP的快速指南
  5. C++(STL):22 ---序列式容器queue使用
  6. java c标签 if有值_c标签 if else c标签 总结
  7. Google Maps API 调用实例
  8. C# SuperSocket服务端入门(一)
  9. linux中id命令的功能,linux中的id命令
  10. 支持全文搜索的桌面搜索工具
  11. ITIL学习(四) 服务、IT服务、服务台管理、IT服务管理
  12. 如何证明pi是无理数
  13. 计算机小学期实践报告,小学期计算机实践报告
  14. SAP中常见的Debug技巧(02)-跳过代码执行
  15. 【Redis】Redis数据库
  16. 03.JavaScript-数据类型和数据类型转换
  17. 用c语言实现扫雷小游戏。
  18. 计算机图形学结课论文,计算机图形学基础教程结课论文
  19. @Transactional 详解 示例
  20. pos机骗局收取押金如何投诉-真实案列解答

热门文章

  1. Python开发第一篇 基础篇(下)
  2. mysql环境变量的配置
  3. 如何做研究-精辟分析
  4. C# 图片处理之:彩色图片转为黑白图 .
  5. PetShop 4.0 SQLHelper
  6. (87)FPGA面试题-同步FIFO与异步FIFO区别?异步FIFO代码设计
  7. android xpath解析xml,Android 中处理 XML 的四种方式-DOM
  8. 用标准C语言初始化线性表,C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作...
  9. 继续教育c语言试题及答案,2006--2007学年西北师范大学继续教育学院《C语言程序设计》试卷B...
  10. linux sort多磁盘排序,linux shell sort多字段排序