【习题3-2】 UVA - 1586 Molar mass
来源:互联网 发布:无锡加工中心编程招聘 编辑:程序博客网 时间:2024/05/16 10:31
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'.
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$ \le$n$ \le$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
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$ \le$n$ \le$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
大致思路为记录C、H、N、O分别出现多少次,最后累加到一起即可。。
AC代码:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;char s[200];char *p = "CHON";double score[] = {12.01, 1.008, 16.00, 14.01};int main(){ int T, len, c[5], temp, i, j; char *f; double sum; cin >> T; while(T--) { cin >> s; len = strlen(s); memset(c, 0, sizeof(c)); temp = 0; for(i = 0; i < len; i+=j) { f = strchr(p, s[i]); if(f) { for(j = 1, temp = 0; !strchr(p, s[i + j]); j++) { temp = temp * 10 + s[i + j] - '0'; } if(!temp) temp = 1; c[f - p] += temp; } } //将所有分子量全部累加起来 for(i = 0, sum = 0.0;i < 4;i++) { sum += c[i] * score[i]; } printf("%.3f\n", sum); } return 0;}
0 0
- 【习题3-2】 UVA - 1586 Molar mass
- 习题3-2 UVa 1586 Molar Mass
- 小紫书 习题 3-2(UVA 1586) 分子量(Molar mass)
- uva 1586 - Molar mass
- UVa 1586 - Molar mass
- UVa 1586 - Molar mass
- UVA 1586 ----Molar mass
- UVA 1586 - Molar mass
- UVa 1586 Molar mass
- UVa 1586 - Molar Mass
- UVA - 1586 Molar mass
- uva 1586 - Molar mass
- UVA 1586 Molar mass
- UVa 1586 - Molar mass
- UVa-1586Molar mass
- 【Uva 1586】 Molar mass
- UVA - 1586 Molar mass
- uva 1586 - Molar mass
- 第十六周项目二——用指针玩字符串(2.去除字符串str中的特定字符c)
- OpenCV学习笔记一 例程
- 在android下运行arm gcc工具链编译出来的可执行文件
- 第十六周项目二用指针玩字符串任务一:字符串连接的完成(用指针作形参)
- 遍历Map的几种方式
- 【习题3-2】 UVA - 1586 Molar mass
- 安卓系统与苹果系统的区别
- POJ 2386 解题报告
- [HDOJ]2014
- 学习boost::asio(1)
- ubuntu上编译Android源码(Build Android source
- 第十六周项目二 用指针玩转字符串-项目目录
- 把苹果iPhone手机传文件传给安卓系统手机
- Codeforces Round #282 (Div. 2) C