一道小模拟——洛谷 P4711 「化学」相对分子质量

传送门

这一行为了便于点击链接(否则会触发标题鼠标特效)

屠龙宝刀点击就送

这一行为了便于点击链接(否则会触发标题鼠标特效)

题意

给出分子式,求相对分子质量。

可能是水合物。

但括号(指的是一般化学意义上的括号)没有嵌套。

关于

一百行的模拟我都要想一年,我真的菜啊

但是练练细心和码风还是相当有意义的

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

string his;
int water,ans;

inline int getint(int &x){//从x起是数 
	if(x==water&&his[x]=='H')return 1;
	if(x>=his.size())return 0;
//	cout<<"getint"<<x<<endl;
	int fuck=0;
	while(his[x]>='0'&&his[x]<='9'){
		fuck=fuck*10+his[x]-'0';
		x++;
	}
	return fuck;
}

inline int getm(char a){
//	cout<<"getm"<<a<<endl;
	if(a=='H')return 2;
	if(a=='C')return 24;
	if(a=='N')return 28;
	if(a=='O')return 32;
	if(a=='F')return 38;
	if(a=='P')return 62;
	if(a=='S')return 64;
	if(a=='K')return 78;
	if(a=='I')return 254;
}


inline int getm(char a,char b){
//	cout<<"getm"<<a<<b<<endl;
	if(a=='N'&&b=='a')return 46;
	if(a=='M'&&b=='g')return 48;
	if(a=='A'&&b=='l')return 54;
	if(a=='S'&&b=='i')return 56;
	if(a=='C'&&b=='l')return 71;
	if(a=='C'&&b=='a')return 80;
	if(a=='M'&&b=='n')return 110;
	if(a=='F'&&b=='e')return 112;
	if(a=='C'&&b=='u')return 128;
	if(a=='Z'&&b=='n')return 130;
	if(a=='A'&&b=='g')return 216;
	if(a=='B'&&b=='a')return 274;
	if(a=='H'&&b=='f')return 357;
	if(a=='P'&&b=='t')return 390;
	if(a=='A'&&b=='u')return 394;
	if(a=='H'&&b=='g')return 402;
}

inline int getlow(int &x){
	if(x==his.size())return 1;
//	cout<<"getlow"<<x<<endl;
	x+=2;
	int fuck=getint(x);
	x++;
	return fuck;
}

inline int calele(int &x){//Ca_{2}
	if(x==his.size())return 0;
//	cout<<"calele"<<x<<endl;
	int fuck;
	if(his[x+1]>='a'&&his[x+1]<='z'){
		fuck=getm(his[x],his[x+1]);
		x+=2;
	}
	else {
		fuck=getm(his[x]);
		x++;
	}
	if(his[x]=='_')fuck*=getlow(x);
	return fuck;
}

inline int calsec(int &x){//Ca_{2}或 (Ca_{2}K_{2})_{2}
	if(x==his.size())return 0;
//	cout<<"calsec"<<x<<endl;
	int fuck=0;
	if(his[x]=='('){
		x++;
		while(his[x]>='A'&&his[x]<='Z')fuck+=calele(x);
		x++;
		if(his[x]=='_')fuck*=getlow(x);
		return fuck;
	}
	else return calele(x);
}

int main(){
	cin>>his;
	water=his.find('~');
	if(water==-1)water=his.size();
	for(int i=0;i<water;)ans+=calsec(i);
	//第12个点WA笔记:H2O前面可能没有系数 
	water++;int fuck=ans+36*getint(water);
	if(fuck&1)printf("%d.5\n",fuck>>1);
	else printf("%d\n",fuck>>1);
}
posted @ 2019-11-12 23:18  Y15BeTa  阅读(140)  评论(0编辑  收藏  举报