分析:简单题,对字符串判断与操作。
Source:
#include<stdio.h>
#include<string.h>
int main()
{
double saw[4]={12.01,1.008,16.00,14.01},sum;
int num[4]={0,0,0,0};
int i,k,l,t,c;
char s[80];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
l=strlen(s);
sum=0;
num[0]=num[1]=num[2]=num[3]=0;
for(i=0;i<l;)
{
if(s[i]=='C'||s[i]=='H'||s[i]=='O'||s[i]=='N')
{
if(s[i]=='C') k=0;
else if(s[i]=='H') k=1;
else if(s[i]=='O') k=2;
else k=3;
if(s[i+1]=='C'||s[i+1]=='H'||s[i+1]=='O'||s[i+1]=='N'||s[i+1]=='\0')
num[k]++;
i++;
}
else
{
if(s[i+1]>='0'&&s[i+1]<='9')
{
num[k]+=10*(s[i]-'0')+s[i+1]-'0';
i+=2;
}
else
{
num[k]+=s[i]-'0';
i++;
}
}
}
for(i=0;i<4;i++)
if(num[i])
sum+=saw[i]*num[i];
printf("%.3lf\n",sum);
}
return 0;
}