P4711 「化学」相对分子质量

P4711 「化学」相对分子质量

给你一个字符串让你输出相对分子质量(弱智字符串模拟)

我比赛tm调了两个半小时啊QAQ

希望以后能增加代码力吧,纪念挂代码

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cstring>
#include<map>
typedef long long LL;
using namespace std;
int RD(){
    int out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
const int maxn = 119;
map<string,double>M;
char s[maxn];
double ans,len;
double now;//
int main(){
    string x = "H";M[x] = 1;
    x = "C";M[x] = 12;
    x = "N";M[x] = 14;
    x = "O";M[x] = 16;
    x = "F";M[x] = 19;
    x = "Na";M[x] = 23;
    x = "Mg";M[x] = 24;
    x = "Al";M[x] = 27;
    x = "Si";M[x] = 28;
    x = "P";M[x] = 31;
    x = "S";M[x] = 32;
    x = "Cl";M[x] = 35.5;
    x = "K";M[x] = 39;
    x = "Ca";M[x] = 40;
    x = "Mn";M[x] = 55;
    x = "Fe";M[x] = 56;
    x = "Cu";M[x] = 64;
    x = "Zn";M[x] = 65;
    x = "Ag";M[x] = 108;
    x = "I";M[x] = 127;
    x = "Ba";M[x] = 137;
    x = "Hf";M[x] = 178.5;
    x = "Pt";M[x] = 195;
    x = "Au";M[x] = 197;
    x = "Hg";M[x] = 201;
    /////////
    cin>>s;len = strlen(s);
    for(int i = 0;i < len;i++){
        if(s[i] == '~'){//水合物
            i++;
            int numw = 0;
            while(s[i] <= '9' && s[i] >= '0'){numw = numw * 10 + s[i] - '0';i++;}
            ans += 18 * numw;
            if(numw == 0)ans += 18;
            break;
            }
        if(s[i] <= 'Z' && s[i] >= 'A'){//处理单个元素
            x = s[i];string temp;
            if(s[i + 1] >= 'a' && s[i + 1] <= 'z')temp = s[i + 1],i++;//是否为两个字母
            x = x + temp;//cout<<"x="<<x<<endl;
            for(map<string,double>::iterator it = M.begin(); it != M.end(); it++){
                string com = it->first;
                if(x == com){now = it->second;break;}
                }
            int tim = 0;
            if(s[i + 1] == '_'){
                while(s[i] != '{')i++;i++;//读取倍数
                while(s[i] != '}'){tim = tim * 10 + s[i] - '0';i++;}
                }
            if(tim)ans += tim * now;
            else ans += now;
            }
        if(s[i] == '('){
            i++;
            double ans1 = 0,now1 = 0;
            char n[maxn];int in = 0;
            while(s[i] != ')'){n[in++] = s[i++];}i++;
            for(int k = 0;k < in;k++){
                if(n[k] <= 'Z' && n[k] >= 'A'){
                    string a,b;a = n[k];
                    if(n[k + 1] <= 'z' && n[k + 1] >= 'a'){b = n[k + 1];k++;}
                    a = a + b;//cout<<a<<endl;
                    for(map<string,double>::iterator it = M.begin(); it != M.end(); it++){
                        string com = it->first;
                        if(com == a){now1 = it->second;break;}
                        }//printf("now1=%d
",now1);
                    int tim1 = 0;	
                    if(n[k + 1] == '_'){
                        while(n[k] != '{')k++;k++;//读取倍数
                        while(n[k] != '}'){tim1 = tim1 * 10 + n[k] - '0';k++;}
                        }
                    if(tim1)ans1 += tim1 * now1;
                    else ans1 += now1;
                    }
                }//printf("ans1=%d
",ans1);
            if(s[i] == '_'){
                int tim2 = 0;
                while(s[i] != '{')i++;i++;
                while(s[i] != '}'){tim2 = tim2 * 10 + s[i] - '0';i++;}
                if(tim2)ans += ans1 * tim2;
                else ans += ans1;
                }
            }
        }
    
    if(ans == (int)ans)printf("%d
",(int)ans);
    else printf("%.1lf",ans);
    return 0;
    }
原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/9330892.html