UVa 1586 - Molar mass
题目是PDF格式
所以这里直接上传送门
写的很复杂,哈哈哈,估计就我自己看得懂
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define C 12.01
#define H 1.008
#define O 16.00
#define N 14.01
int main()
{
int n;
scanf("%d",&n);
char str[100];
getchar();
while(n--)
{
double Sum=0.000;
char s;
scanf("%s",str);
getchar();
int n=strlen(str);
for(int i=0; i<n; i++)
{
if(str[i]=='C')
{
if(str[i+1]>='1'&&str[i+1]<='9')
{
if(str[i+2]>='0'&&str[i+2]<='9')
{
Sum+=C*((str[i+1]-'0')*10+str[i+2]-'0');
}
else
Sum+=C*(str[i+1]-'0');
}
else
Sum+=C;
}
else if(str[i]=='H')
{
if(str[i+1]>='1'&&str[i+1]<='9')
{
if(str[i+2]>='0'&&str[i+2]<='9')
{
Sum+=H*((str[i+1]-'0')*10+str[i+2]-'0');
}
else
Sum+=H*(str[i+1]-'0');
}
else
Sum+=H;
}
else if(str[i]=='O')
{
if(str[i+1]>='1'&&str[i+1]<='9')
{
if(str[i+2]>='0'&&str[i+2]<='9')
{
Sum+=O*((str[i+1]-'0')*10+str[i+2]-'0');
}
else
Sum+=O*(str[i+1]-'0');
}
else
Sum+=O;
}
else if(str[i]=='N')
{
if(str[i+1]>='1'&&str[i+1]<='9')
{
if(str[i+2]>='0'&&str[i+2]<='9')
{
Sum+=N*((str[i+1]-'0')*10+str[i+2]-'0');
}
else
Sum+=N*(str[i+1]-'0');
}
else
Sum+=N;
}
}
printf("%.3f\n",Sum);
}
return 0;
}