今天感觉收获比较大,《算法竞赛入门经典》看了几个例题,接触到很多以前不知道的知识,还是比较开心的。下面讲讲做的一个题,能力有限,还有很多
知识不知道,不了解,代码写得非常水,起码是正确的
An organic compound is any member of a large class of chemicalcompounds whose molecules contain carbon. The molarmass of an organic compound is the mass of one mole of theorganic compound. The molar mass of an organic compoundcan be computed from the standard atomic weights of theelements.When an organic compound is given as a molecular formula,Dr. CHON wants to find its molar mass. A molecularformula, such as C3H4O3, identifies each constituent element byits chemical symbol and indicates the number of atoms of eachelement found in each discrete molecule of that compound. Ifa 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 NitrogenStandard 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 C6H5OH is 94.108 g/mol which is computed by6 × (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.InputYour program is to read from standard input. The input consists of T test cases. The number of testcases T is given in the first line of the input. Each test case is given in a single line, which containsa molecular formula as a string. The chemical symbol is given by a capital letter and the length ofthe string is greater than 0 and less than 80. The quantity number n which is represented after thechemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).OutputYour program is to write to standard output. Print exactly one line for each test case. The line shouldcontain the molar mass of the given molecular formula.Sample Input4CC6H5OHNH2CH2COOHC12H22O11Sample Output12.01094.10875.070342.296
#include<stdio.h>
int f(char ch);
int main()
{
char s[100];
int i,T;
scanf("%d",&T);
getchar();
while(T--)
{
double c=12.01,h=1.008,o=16.00,n=14.01,t=0.0;
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='C')
{
int a1=1;
if(s[i+1]>='1'&&s[i+1]<='9')
a1=f(s[i+1]);
if(s[i+1]>='1'&&s[i+1]<='9'&&s[i+2]>='1'&&s[i+2]<='9')
a1=a1*10+f(s[i+2]);
t+=c*a1;
}
if(s[i]=='H')
{
int a1=1;
if(s[i+1]>='1'&&s[i+1]<='9')
a1=f(s[i+1]);
if(s[i+1]>='1'&&s[i+1]<='9'&&s[i+2]>='1'&&s[i+2]<='9')
a1=a1*10+f(s[i+2]);
t+=h*a1;
}
if(s[i]=='O')
{
int a1=1;
if(s[i+1]>='1'&&s[i+1]<='9')
a1=f(s[i+1]);
if(s[i+1]>='1'&&s[i+1]<='9'&&s[i+2]>='1'&&s[i+2]<='9')
a1=a1*10+f(s[i+2]);
t+=o*a1;
}
if(s[i]=='N')
{
int a1=1;
if(s[i+1]>='1'&&s[i+1]<='9')
a1=f(s[i+1]);
if(s[i+1]>='1'&&s[i+1]<='9'&&s[i+2]>='1'&&s[i+2]<='9')
a1=a1*10+f(s[i+2]);
t+=n*a1;
}
}
printf("%.3lf\n",t);
}
return 0;
}
int f(char ch)
{
if(ch=='1')
return 1;
if(ch=='2')
return 2;
if(ch=='3')
return 3;
if(ch=='4')
return 4;
if(ch=='5')
return 5;
if(ch=='6')
return 6;
if(ch=='7')
return 7;
if(ch=='8')
return 8;
if(ch=='9')
return 9;
}