uvaoj-1586:分子量
2016-02-12 14:27
260 查看
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,
for
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≤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
题解:简单的模拟题,在lrj的书上这种题算是简单的不可思议的题目了,(主要是我被虐的太多次了)。。
但是讽刺的是没能一次ac,因为忘记保留小数了,cout默认是保留两位小数,记错成三位了,下面是代码,也许不够优化,但绝对是正确的;
code:
#include <iostream>
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
char alp[5]="CHON";
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;
}
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), andN’ (Nitrogen) without parentheses.The following table shows that the standard atomicweights
for
C',H’,
O', andN’.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≤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
题解:简单的模拟题,在lrj的书上这种题算是简单的不可思议的题目了,(主要是我被虐的太多次了)。。
但是讽刺的是没能一次ac,因为忘记保留小数了,cout默认是保留两位小数,记错成三位了,下面是代码,也许不够优化,但绝对是正确的;
code:
#include <iostream>
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
char alp[5]="CHON";
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;
}
相关文章推荐
- On-Premise
- Dreamweaver代码提示快捷键冲突
- Hibernate第一个demo及工作原理
- Java认证考试实例疑难辨析(7)
- Educational Codeforces Round 7 A. Infinite Sequence 水题
- 如何查找阅读综述性的文章
- Eclipse调试程序的方法
- mongodb副本集
- 尺寸单位em,rem,vh,vw
- POJ 2629 Common permutation(水~)
- STL vector容器需要警惕的一些坑
- 一种简单的缓冲动画函数实现
- BZOJ2843: 极地旅行社
- 分享两种筛选质因数的方法
- POJ 2627 Gopher and hawks(bfs)
- 动态数组类
- 时间日期
- C语言——数据类型,运算符,表达式
- Windows环境Apache,Tomcat集群,动静分离,负载均衡
- POJ 2626 Chess(dp)