这题做的相当的复杂。。。之前做的现在应该能简单一点了写的。

我的代码:

#include <bits/stdc++.h>
using namespace std;
main()
{
int a;
cin>>a;
while(a--)
{
string b="";
cin>>b;
stringstream stream;
int sumc,sumh,sumo,sumn;
sumc=sumh=sumo=sumn=;
int c,h,o,n,f;
c=h=o=n=;f=;
string str="";
for(int i=;i<b.size()+;i++)
{
if(isalpha(b[i])&&isalpha(b[i+])||isalpha(b[i])&&!b[i+])
{
if(b[i]=='C')
sumc+=;
if(b[i]=='H')
sumh+=;
if(b[i]=='O')
sumo+=;
if(b[i]=='N')
sumn+=;
}
else if(isdigit(b[i])&&f)
{
if(b[i-]=='C')
{
str+=b[i];c=;f=;
}
if(b[i-]=='H')
{
str+=b[i];h=;f=;
}
if(b[i-]=='O')
{
str+=b[i];o=;f=;
}
if(b[i-]=='N')
{
str+=b[i];n=;f=;
}
}
else if(c&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;c=;
stream<<str;
stream>>num;
sumc+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
else if(h&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;h=;
stream<<str;
stream>>num;
sumh+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
else if(o&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;o=;
stream<<str;
stream>>num;
sumo+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
else if(n&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;n=;
stream<<str;
stream>>num;
sumn+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
}
double sum=;
sum=sumc*12.01+sumh*1.008+sumo*16.00+sumn*14.01;
printf("%.3lf\n",sum);
}
}
05-17 12:22