您的位置:首页 > 其它

【白书之路】 1586 - Molar mass 字符串分离处理

2015-09-04 16:48 537 查看
1586 - Molar mass

Anorganiccompound  isanymemberofalargeclassofchem-
ical compounds whose molecules contain carbon. The  molar
mass ofanorganiccompoundisthemassofonemoleofthe
organiccompound. Themolarmassofanorganiccompound
can be computed from the standard atomic weights of the
elements.


When an organic compound is given as a molecular for-
mula,Dr. CHONwantstofinditsmolarmass.     Amolecular
formula,suchasC3H4O3,identifieseachconstituentelementby
itschemicalsymbolandindicatesthenumberofatomsofeach
elementfoundineachdiscretemoleculeofthatcompound.     If
a molecule contains more than one atom of a particular ele-
ment,thisquantityisindicatedusingasubscriptafterthechemicalsymbol.
In this problem, we assume that the molecular formula is represented by only four elements, ‘C’
(Carbon),‘H’(Hydrogen),‘O’(Oxygen),and‘N’(Nitrogen)withoutparentheses.
Thefollowingtableshowsthatthestandardatomicweightsfor‘C’,‘H’,‘O’,and‘N’.
AtomicName
Carbon
Hydrogen
Oxygen
StandardAtomicWeight   12.01g/mol   1.008g/mol  16.00g/mol   14.01g/mol
Nitrogen
Forexample,themolarmassofamolecularformulaC6H5OHis94.108g/molwhichiscomputedby
6×(12.01g/mol)+6×(1.008g/mol)+1×(16.00g/mol).
Givenamolecularformula,writeaprogramtocomputethemolarmassoftheformula.
Input
Yourprogramistoreadfromstandardinput.     TheinputconsistsofT   testcases. Thenumberoftest
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
chemicalsymbolwouldbeomittedwhenthenumberis1(2≤n≤99).
Output
Yourprogramistowritetostandardoutput.    Printexactlyonelineforeachtestcase.     Thelineshould
containthemolarmassofthegivenmolecularformula.
Sample Input
4
C
C6H5OH
NH2CH2COOH
C12H22O11
Sample Output
12.010
94.108
75.070
342.296
只要是上过高中,算摩尔质量神马的都是浮云,这里有两个关键:

    1.如何在O(1)的时间复杂度内得到一个原子的质量

    2.如何将字符串中的数字分离出来

    对于第一个问题,可以使用简单的哈希处理,将字母的ASCII码与数组下标对应起来,从而可以获取单个原子的摩尔质量。

    对于第二个问题,我使用边遍历边计算的方式,比如对于一个数字字符串"1234",我们先定义一个变量num=0,此时指针p=0,首先计算num=num*10,然后计算num=num+str[i]-'0',i++,重复这个过程,即可将整个字符串变为数字。

    当然,也可以使用如atoi()或者sscanf()之类的函数进行转换。

    最后,将所有的质量相加即可得到最终的摩尔质量。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 90
using namespace std;

double mass[13];
char formula[MAX];
int len;

bool is_uper(char c)//判断是否为大写字母
{
if(c>='A'&&c<='Z')
return true;
else
return false;
}
bool is_digit(char c)//判断是否为数字
{
if(c>='0'&&c<='9')
return true;
else
return false;
}

void init()//初始化
{
mass[0]=12.01;
mass[5]=1.008;
mass[11]=14.01;
mass[12]=16.00;

}
int main()
{
init();
int t,i,j;
double atom_mass;//原子质量
double molar_mass;//摩尔质量
int quantity;
scanf("%d",&t);
while(t--)
{
scanf("%s",formula);
len=strlen(formula);
molar_mass=0;
for(i=0; i<len;)//遍历字符串
{
if(is_uper(formula[i]))//如果是大写字母
{

atom_mass=mass[formula[i]-'C'];//计算这个原子的质量
//printf("atom mass=%lg\n",atom_mass);
i++;
quantity=0;
while(is_digit(formula[i]))//数字分离
{
quantity*=10;
quantity+=formula[i]-'0';
i++;
}
//printf("quantity=%d\n",quantity);
if(quantity==0)
quantity=1;
molar_mass+=atom_mass*quantity;//计算摩尔质量
}
}
printf("%.3f\n",molar_mass);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息