Molar mass UVA - 1586
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的更多相关文章
- 【OI】计算分子量 Molar mass UVa 1586 题解
题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...
- UVa 1586 Molar mass --- 水题
UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)
习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同 ...
- UVa 1586 Molar mass
题意:给出物质的分子式,计算它的相对原子质量 因为原子的个数是在2到99之间的,所以找出一个是字母之后,再判断一下它的后两位就可以找出这种原子的个数了 #include<iostream> ...
- 分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf ...
- UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言
关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...
- uva 1586 Molar mass(Uva-1586)
这题做的相当的复杂...之前做的现在应该能简单一点了写的. 我的代码: #include <bits/stdc++.h> using namespace std; main() { int ...
- 【习题 3-2 UVA - 1586】Molar mass
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 主要是找那个数字. [代码] #include <bits/stdc++.h> using namespace ...
随机推荐
- windows(wsl)下的trino编译和升级注意事项
最近在进行旧版本的prestosql和prestodb升级相关的操作,尝试自己编译了一下,这里记录一下过程和遇到问题的处理. 因为Trino不支持windows下的编译,如果使用windows最方便的 ...
- ConcurrentHashMap允许一边遍历一边更新,而用HashMap则会报线程安全问题
ConcurrentHashMap线程安全的,允许一边更新.一边遍历,也就是说在对象遍历的时候,也可以进行remove,put操作,且遍历的数据会随着remove,put操作产出变化,而如果用Hash ...
- 如何删除Image元素下面的空白行及为什么行内元素有底线
翻译练习 原博客地址:Removing White Space Below Image Elements, or Why Inline Elements Have Descenders HTML中Im ...
- 《Linux学习笔记:文本编辑最佳实践》
[Linux文本编辑的四种方法] 例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 [推荐] 打开终端,输入vi t ...
- XAPKInstaller - XAPK游戏包安装器
XAPKInstaller 一个用于安装XAPK游戏包的安装器. 程序需要读写存储与获取已安装应用权限才可正常运行. 长按条目可显示文件的详细信息. SDK小于24(Android N)的设备会显示应 ...
- Salesforce LWC学习(三十一) Quick Action适配
本篇参考:https://www.lightningdesignsystem.com/components/modals/ 随着salesforce lwc的优化,越来越多的项目从aura转到了lwc ...
- Java语言的魅力
Java语言的简介 Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态面 ...
- HDOJ-1114(完全背包模板题)
Piggy-Bank HDOJ-1114 本题就是完全背包的模板题,注意复习一下关于背包九讲中的问什么这里使用的是顺序遍历. 还需要注意的一个问题就是初始化的问题,dp[0]初始化为0,其他的初始化为 ...
- SpringBoot启动流程原理解析(二)
在上一章我们分析了SpingBoot启动流程中实例化SpingApplication的过程. return new SpringApplication(primarySources).run(args ...
- 死磕Spring之IoC篇 - @Autowired 等注解的实现原理
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...