CSP201912-3 chemical equation

Topic see https://blog.csdn.net/wingrez/article/details/103551680 If you don't find the entrance, you won't have an onli...

Topic see https://blog.csdn.net/wingrez/article/details/103551680

If you don't find the entrance, you won't have an online test. The sample has passed

Idea: at the beginning, I have been trying to find a more reasonable way, but I haven't thought of a suitable way. At last, I have to deal with one character at a time. Each time I deal with an element and its number of atoms are completed by getnumber and getele respectively. I deal with parentheses recursively

EEE structure is defined to represent the element composition of a compound. Addition and multiplication are simply defined to calculate the atomic number;

As a whole, we have simulated the calculation of calculation expression once, but the number has become a chemical formula

#include "bits/stdc++.h" #include <cstring> #include <cstdlib> #include <cstdio> #define mk(a,b) make_pair(a,b) using namespace std; #define M 1003 /* in 11 H2+O2=H2O 2H2+O2=2H2O H2+Cl2=2NaCl H2+Cl2=2HCl CH4+2O2=CO2+2H2O CaCl2+2AgNO3=Ca(NO3)2+2AgCl 3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2 3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O 4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O 4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH Cu+As=Cs+Au >> N Y N Y Y Y Y Y Y Y N */ struct EEE{ map<string,int> e; void re_ele(string ele,int cnt=1){ e.clear(); e[ele]=cnt; } void add(string ele,int cnt=1){ EEE te; te.re_ele(ele,cnt); *this+te; } EEE operator *(int b){ for(auto it=e.begin();it!=e.end();it++){ it->second=it->second*b; } return *this; } EEE operator+(EEE &b){ for(auto it=b.e.begin();it!=b.e.end();it++){ auto i=e.find(it->first); if(i!=e.end()){ i->second+=it->second; }else{ e[it->first]=it->second; } } return *this; } }; int getnumber(char *&p){ int cnt=-1; if(isdigit(*p)){ cnt=*p-'0'; p++; while(isdigit(*p)){ cnt=cnt*10+*p-'0'; p++; } } return cnt; } string getele(char *&p){ string ele="#"; if(isupper(*p)){ ele=""; ele+=*p;p++; if(islower(*p)){ ele+=*p;p++; } } return ele; } void showE(EEE &t){ for(auto &i:t.e){ cout<<i.first<<i.second<<endl; } cout<<endl; } EEE itemdis(char *&s){ //cout<<s<<endl; EEE re,te; string ele="#"; int cnt,tn; cnt=getnumber(s); while(*s!='\0'){ if(*s=='('){ s++; EEE te=itemdis(s); tn=getnumber(s); if(tn>0)te*tn; re+te; }else if(*s==')'){ s++; return re; }else { ele=getele(s); tn=getnumber(s); if(tn==-1)tn=1; re.add(ele,tn); } } if(cnt>0)re*cnt; // showE(re); return re; } void eqcount(EEE &e,char *s){ char *p=s; EEE te; while(p=strchr(s,'+')){ *p='\0';p++; te=itemdis(s); e+te; s=p; } te=itemdis(s); e+te; } void eqcheck(EEE &l,EEE &r){ // showE(l),showE(r); cout<<"ans:"; bool ans = true; if(l.e.size()!=r.e.size()) ans=false; if(ans){ for(auto ita=l.e.begin(),itb=r.e.begin();ita!=l.e.end();ita++,itb++){ if(ita->first!=itb->first || ita->second!=itb->second){ ans = false; break; } } } printf("%c\n",ans?'Y':'N'); } void solve(){ /* Test of atomic conservation of chemical equation */ int cnt; char eqstr[103],*pl,*pr; cin>>cnt; while(cnt--){ scanf("%s",eqstr); pl=eqstr; pr=strchr(eqstr,'='); *pr='\0'; pr++; EEE l,r; eqcount(l,pl); eqcount(r,pr); eqcheck(l,r); } } int main() { freopen("in","r",stdin); //freopen("out","w",stdout); solve(); return 0; }

M_H5211 Published 49 original articles, won praise 4, visited 10000+ Private letter follow

8 February 2020, 11:43 | Views: 8650

Add new comment

For adding a comment, please log in
or create account

0 comments