UVa1586,Molar Mass

#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);
 }
}

你可能感兴趣的:(uva)