uvaoj-1586:分子量
来源:互联网 发布:java id 生成 与还原 编辑:程序博客网 时间:2024/04/29 10:13
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 C3 H4 O3 , identifies each constituent element by its chemicalsymbol 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 atomicweights 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/molFor example, the molar mass of a molecular formula C6 H5 OH 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 readfrom 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 chemicalsymbol 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
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
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
double g[4]={12.01,1.008,16.00,14.01};
int main()
{
int t;
cin>>t;
char s[1005];
while(t--)
{
cin>>s;
int len=strlen(s);
int n=0;//计次器;
double ans=0;//累加器;
for(int i=0; i<len; i++)
{
for(int j=0; j<4; j++)
{
if(s[i]==alp[j])
{
if(isupper(s[i+1])||s[i+1]=='\0')//因为为下标1的时候会省略,所以用这种方法来判定;
{
ans+=g[j];
continue;
}
else
{
int k=i+1;//用这个方法纯粹是因为不喜欢直接改循环变量,用另外的变量来代替可以减少错误率;
while(isdigit(s[k]))
{
n=n*10+s[k]-'0';
k++;
}
i=k-1;
ans+=(n*1.0)*g[j];
n=0;//重新赋值;
break;
}
}
}
}
cout<<fixed<<setprecision(3)<<ans<<endl;//保留小数点的方法,但是不推荐。。
}
return 0;
}
- uvaoj-1586:分子量
- Uva 1586 分子量
- UVA 1586 分子量
- UVA-1586分子量
- uva 1586 分子量
- 分子量
- 分子量
- 分子量
- UVa 1586 Molar Mass(分子量)
- UVa 1586 Molar mass(分子量)
- UVa 1586 Molar mass (计算分子量,不含扩号)
- UVA 1586 分子量(简单模拟计算)
- 分子量(Molar Mass,UVa 1586)水题
- UVA 1586 分子量(Molar Mass)
- uvaoj 1586-molar mass
- UVaOJ
- 小紫书 习题 3-2(UVA 1586) 分子量(Molar mass)
- uva1586分子量
- 如何查找阅读综述性的文章
- 温江大庙会2016年门票 巴适票务承包特价
- Java认证考试实例疑难辨析(7)
- html5 网络状态
- On-Premise
- uvaoj-1586:分子量
- java基础20递归
- Building Maintainable Software-java篇之Write Simple Units of Code
- sdptool 设备服务查找命令使用
- [I0A]查找最大子串问题的求解
- IIC驱动学习
- git 操作技巧
- 【LA7402】colorful tree 数据结构
- 工作笔记-2014.4.3