这题做的相当的复杂。。。之前做的现在应该能简单一点了写的。
我的代码:
#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=0; int c,h,o,n,f; c=h=o=n=0;f=1; string str=""; for(int i=0;i<b.size()+1;i++) { if(isalpha(b[i])&&isalpha(b[i+1])||isalpha(b[i])&&!b[i+1]) { if(b[i]==‘C‘) sumc+=1; if(b[i]==‘H‘) sumh+=1; if(b[i]==‘O‘) sumo+=1; if(b[i]==‘N‘) sumn+=1; } else if(isdigit(b[i])&&f) { if(b[i-1]==‘C‘) { str+=b[i];c=1;f=0; } if(b[i-1]==‘H‘) { str+=b[i];h=1;f=0; } if(b[i-1]==‘O‘) { str+=b[i];o=1;f=0; } if(b[i-1]==‘N‘) { str+=b[i];n=1;f=0; } } else if(c&&!f) { if(isalpha(b[i])||!b[i]) { int num=0; f=1;c=0; 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=0; f=1;h=0; 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=0; f=1;o=0; 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=0; f=1;n=0; stream<<str; stream>>num; sumn+=num; stream.clear(); str=""; } else str+=b[i]; } } double sum=0; sum=sumc*12.01+sumh*1.008+sumo*16.00+sumn*14.01; printf("%.3lf\n",sum); } }
原文:https://www.cnblogs.com/baccano-acmer/p/9736061.html