[LuoguP4711]分子质量(小模拟+玛丽题)

……这个题我居然可以把他\(1A\)……真是让我不知其可\(qwq\)

\(Link\)

\(emmmm\)好像发现了什么固定的套路(?)……

大概就是这种题总会有

1、读入数 方案:快读即可。

2、鬼畜的优先级 方案:只要代码敲不死,就往死里敲代码\(qwq\)——特判

3、“数 + 数据”的运算 方案:跟着感觉走……

\(emmm\)第三条是真的!不是水水!这道题里的关键部分(即小括号的判读以及运算)由于是后缀形式,所以需要多操作一下\(qwq\)但我并没有特别缜密的思考就给搞完了

诶?被你发现了?其实我就是来存个代码的\(qwq\),甚至连题目描述都没有……

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>

int flag = 0 ;
using namespace std ;
const int MAXN = 210 ;
map<string, double> a ;
string s, qwq ; int N, i ;
double ans, res, Last, j ;

inline void init(){
    a["H"]=1; a["C"]=12; a["N"]=14; a["O"]=16;
    a["F"]=19; a["Na"]=23; a["Mg"]=24; a["Al"]=27;
    a["Si"]=28; a["P"]=31; a["S"]=32; a["Cl"]=35.5;
    a["K"]=39; a["Ca"]=40; a["Mn"]=55; a["Fe"]=56;
    a["Cu"]=64; a["Zn"]=65; a["Ag"]=108; a["I"]=127;
    a["Ba"]=137; a["Hf"]=178.5; a["Pt"]=195; a["Au"]=197;
    a["Hg"]=201;
}
inline int r_int(int x, int &q){
    res = 0 ;
    while(s[x] <= '9' && s[x] >= '0')
        res = res * 10 + s[x] - 48, x ++ ;
    q = x ;
    return res ;
}
inline double r_char(int x, int &q){
    double ret = 0, T ; bool mark = 0 ;
    if(x && s[x - 1] == '(') flag ++, mark = 1 ;
    while(x <= N && (s[x] > '9' || s[x] < '0') && s[x] != '~' ){
        if(s[x] == '(') x += 1, ret += r_char(x, x) ;
        if(s[x] == ')') {x ++ ; break ;}
        if(s[x + 1] <= 'z' && s[x + 1] >= 'a')
            {string now ; now = now + s[x] + s[x + 1]; x ++, ret += a[now], qwq = now ;}
        else if(s[x] <= 'Z' && s[x] >= 'A') {string now ; now += s[x] ; qwq = now, ret += a[now] ;}
        else if(s[x] == '_'){
            x += 2, T = r_int(x, x) ;
            if(!flag || mark)ret += a[qwq] * (T - 1) ;
            else {
                flag -- ;
                ret += (T - 1) * Last ;
            }
        }
        x ++ ;
    }
    q = x ; if(flag) Last = ret ;
    return ret ;
}
int main(){
    cin >> s ; N = s.size() ;init() ;
    for(i = 0; i <= N ; i ++){
        if(s[i] == '~') continue ;
        j = r_int(i, i) ; if(!j) j = 1.0 ;
        ans += r_char(i, i) * j ;
    }
    cout << ans ;
}

posted @ 2018-07-25 10:17  皎月半洒花  阅读(133)  评论(0编辑  收藏  举报