《算法竞赛入门经典》(第2版)习题 3-2 分子量

题目

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

在UVa上的英文原题如下:
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

解题思路

我用string定义的变量来输入字符串,然后读取一个字符,判断并记录之后的原子个数(即数字)。在最后调试的时候,发现一个潜在的BUG,当字符下标遍历到最后一位时,i+1会越界,我就人工设置了一个结束符 ’ \0 '。

对了,还有一件事,关于string越界问题,我知道这个类的结尾是没有规定一定要有结束标志NULL,但是什么时候会越界还是有点不大清楚,具体自己可以查资料。我的思路有错误的话,欢迎指正!

AC代码(UVa的OJ)

#include <iostream>
#include <string>

using namespace std;

double operate(char c, int n) {
	double t = 0;
	if (c == 'C') t = 12.01;
	if (c == 'H') t = 1.008;
	if (c == 'O') t = 16.00;
	if (c == 'N') t = 14.01;
	return t * n;
}

int main() {
	int n;
	cin >> n;
	while (n--) {
		string ss;
		cin >> ss;
		int len = ss.length();
		ss[len] = '\0';	//人工给结尾添加一个结束符,防止后面i+1越界 
		double mass = 0;
		for (int i = 0; i < len+1; i++) {
			char c = ss[i];
			int sign = 0;	//标志数字的位数 
			int number = 1;	//原子数量,初始化为一个 
		
			for (; ss[i+1] >= '0' && ss[i+1] <= '9'; i++) {	 
				sign++;	//表示数字位数 
				if (sign == 1) number = ss[i+1] - '0';
				else number = number*10 + ss[i+1] - '0';	//若不是为一位数,则叠加 
			}
			mass += operate(c,number);
		}	
		printf("%.3lf\n", mass);
	}
	
	return 0;
} 
发布了24 篇原创文章 · 获赞 0 · 访问量 706

猜你喜欢

转载自blog.csdn.net/qq_44296342/article/details/104331034
今日推荐