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;
}
本文提供了一种解决UVa1586-Molarmass问题的方法,通过C语言程序计算分子质量。该程序能够读取化学式并计算其中碳(C)、氢(H)、氧(O)和氮(N)的原子数,进而求得分子的总质量。
308

被折叠的 条评论
为什么被折叠?



