【白书之路】 1586 - Molar mass 字符串分离处理
2015-09-04 16:48
537 查看
1586 - Molar mass
![](https://img-blog.csdn.net/20150904163601310?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
只要是上过高中,算摩尔质量神马的都是浮云,这里有两个关键:
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;
}
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;
}
相关文章推荐
- 【白书之路】 分数化小数 高精度小数运算
- 【白书之路】 排列 permutationy
- 【白书之路】 UVA272 Tex Quates
- 【白书之路】10082 - WERTYU string使用
- 【白书之路】401 - Palindromes 回文串 镜像串
- 【白书之路】340 - Master-Mind Hints 数字统计
- 【白书之路】1583 - Digit Generator
- 【白书之路】1584 - Circular Sequence 最小字典序
- 【白书之路】 1585 - Score 统计分数
- URAL 1586
- HUST 1586 数字排列【暴力枚举】水题
- 【白书之路】 Uva 1587 Box 六个面是否能组成长方体
- 【白书之路】 10340 All in All 判断子串
- 【白书之路】UVa 202 Repeating Decimals 计算小数循环节
- 【白书之路】 1368 - DNA Consensus String 最相似串
- 【白书之路】 232 - Crossword Answers 字符串单词提取
- 【白书之路】 227 - Puzzle 模拟
- 【白书之路】455 - Periodic Strings KMP next 数组 求 循环节
- 【白书之路】1225 - Digit Counting 数字统计
- UVaoj 1586处理输入的一个小问题