【UVA】1586 Molar mass

目录

题目内容:

Input

Output

Sample Input

Sample Output

题目分析: 

代码:


 题目链接 :https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4461

题目内容:


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

题目分析: 

本题主要是求分子式的相对分子质量。此外,注意输入例子中会有C12这种数量不只一位的情况。

可以考虑是否使用数组存输入的分子式。我未使用数组存分子式,直接使用的getchar()获取。主要思路是,判断当前读入的字符为数字还是字母。为数字,则计算个数,乘10 再加当前读取的数字;为字母,则把其前一元素的相对分子质量算入最终结果中。另外,注意读完所有位后,只有最后一位没有再往后的一位,所以,最终需要再把最后一个元素的相对分子质量部分加入结果中。

另外,存每个原子的相对原子质量时,我采用的是 “ 字母减'A' ” 的方式存。但后来看了别人的代码,才发现,直接用字母的字符来存即可。

代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
double m[26];
int main()
{
	int n,tem;
	double ans;
	char a,b=' ';
	memset(m,0,sizeof(m));
	m['C'-'A']=12.01;
	m['H'-'A']=1.008;
	m['O'-'A']=16;
	m['N'-'A']=14.01;
	scanf("%d",&n);
	getchar();//获取\n
	while (n--)
	{
		ans=0;tem=0;
		while ((a=getchar()) != '\n')
		{
			if (isdigit(a))
			{
				tem *= 10;
				tem = tem + (a-'0');
			}
			else if (isalpha(a))
			{
				if (!tem) tem = 1;
				ans = ans + m[b-'A']*tem;
				tem = 0;
				b = a;//存储键入的字母
			}
		}
		if (!tem) tem = 1;
		ans = ans + m[b-'A']*tem;
		b = ' ';
		printf("%.3lf\n",ans);
	}
	return 0;
}

 

全部评论

相关推荐

03-20 12:22
门头沟学院 Java
牛客998737654号:没有hc了吧,但是我接到到后端的面试邀请
投递美团等公司6个岗位
点赞 评论 收藏
分享
牛客501015981号:前面志愿结束了,在大池子里面被其他部门捞了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务