UVa.1586

Output
Your program is to write to standard output. Print exactly one line for each test case. The line should

contain the molar mass of the given molecular formula.


Sample Input
4
C
C6H5OH
NH2CH2COOH

C12H22O11


Sample Output
12.010
94.108
75.070

342.296


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
 double c,h,o,n;
 int t,i,j;
 cin>>t;
 getchar();
 double ans;
 char s[100];
 while (t-->0)
 {
  gets(s);
  c=0;
  h=0;
  o=0;
  n=0;
     int len=strlen(s);
  i=0;
  j=0;
  while(i<len){
   if (s[i]>='A'&&s[i]<='Z'){
    if (s[i+1]>='0'&&s[i+1]<='9')
    {
     j=j*10+s[i+1]-'0';
     if (s[i+2]>='0'&&s[i+2]<='9') j=j*10+s[i+2]-'0';
    } else j=1;
    if(s[i]=='C') c+=j;
    if(s[i]=='H') h+=j;
    if(s[i]=='O') o+=j;
    if(s[i]=='N') n+=j;
    j=0;
   }
   i++;
  }
  printf("%.3lf\n",c*12.01+h*1.008+o*16.00+n*14.01);
 }
}

猜你喜欢

转载自blog.csdn.net/CANGYE0504/article/details/72802446