#include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
int main(){
int t;
double c, h, o, n;
string molecule;
vector <char> container;
cin >> t;
getchar();
while(t--){
double cf = 12.01, hf = 1.008, of = 16.00, nf = 14.01, cc, hh, oo, nn;
c = 0, h = 0, o = 0, n = 0;
cin >> molecule;
int sz = molecule.size();
for(int i = 0; i < sz;){
int j = i+1;
if(molecule[i] =='C' || molecule[i] =='H' || molecule[i] =='O' || molecule[i] =='N'){
while(molecule[j] >= '0' && molecule[j] <= '9')container.push_back(molecule[j]), j++;
int sz1 = container.size();
double sum = 0;
for(int k = 0; k < sz1; k++)sum = (sum*10) + (container[k]-48);
container.clear();
if(sum == 0)sum = 1;
if(molecule[i] == 'C')cc = cf*sum, c += cc;
if(molecule[i] == 'H')hh = hf*sum, h += hh;
if(molecule[i] == 'O')oo = of*sum, o += oo;
if(molecule[i] == 'N')nn = nf*sum, n += nn;
i = j;
}
}
cout << fixed << setprecision(3) << c+h+n+o << endl;
}
return 0;
}
#include <cstdio>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
int main(){
int t;
double c, h, o, n;
string molecule;
vector <char> container;
cin >> t;
getchar();
while(t--){
double cf = 12.01, hf = 1.008, of = 16.00, nf = 14.01, cc, hh, oo, nn;
c = 0, h = 0, o = 0, n = 0;
cin >> molecule;
int sz = molecule.size();
for(int i = 0; i < sz;){
int j = i+1;
if(molecule[i] =='C' || molecule[i] =='H' || molecule[i] =='O' || molecule[i] =='N'){
while(molecule[j] >= '0' && molecule[j] <= '9')container.push_back(molecule[j]), j++;
int sz1 = container.size();
double sum = 0;
for(int k = 0; k < sz1; k++)sum = (sum*10) + (container[k]-48);
container.clear();
if(sum == 0)sum = 1;
if(molecule[i] == 'C')cc = cf*sum, c += cc;
if(molecule[i] == 'H')hh = hf*sum, h += hh;
if(molecule[i] == 'O')oo = of*sum, o += oo;
if(molecule[i] == 'N')nn = nf*sum, n += nn;
i = j;
}
}
cout << fixed << setprecision(3) << c+h+n+o << endl;
}
return 0;
}
No comments:
Post a Comment