201912-3 化学方程式 70分【CCF-CSP真题】

费了九牛二虎之力照着80分写法却只拿了70分,还没来得及写递归
先占坑吧 等写满分了再更

#include<bits/stdc++.h>
using namespace std;
map<string,int> m1,m2;
vector<string> v;
string str;
int find(int i)
{
	int cnt3=0;
	while(1)
	{
		if(str[i]==')') break;
		i++;
	}
	i++;

	while(str[i]>='0'&&str[i]<='9')//取系数 
	{
		cnt3*=10;
		cnt3+=str[i]-'0';
		i++;
	}
	return cnt3;
}

int main()
{
	int t;cin>>t;
	while(t--)
	{
		cin>>str;
		m1.clear();m2.clear();
		v.clear();
		int i=0;
		int cnt1=0,cnt2=0;
		int k=str.find('=');
		while(i<k)
		{
			//cout<<i<<endl;
			if(str[i-1]=='+'||i==0)//到系数之前初始化 
			{
				cnt1=0;
			}
			while(str[i]>='0'&&str[i]<='9')//取系数 
			{
				cnt1*=10;
				cnt1+=str[i]-'0';
				i++;
			}
			int cnt3=0;
			while(1)//取每个元素及其下标 
			{
				
				cnt2=0;
				string s;
				if(str[i]=='(')//取括号外数字 
				{
					cnt3=find(i);
					//cout<<"jump to "<<jmp<<endl;
					//cout<<cnt3<<endl;
					i++;
				}
				if(str[i]>='A'&&str[i]<='Z')//取元素 
				{
					s+=str[i];
					if(str[i+1]>='a'&&str[i+1]<='z')
					{
						s+=str[i+1];
						i++;
					}
				}
				i++;
				while(str[i]>='0'&&str[i]<='9')
				{
					cnt2*=10;
					cnt2+=str[i]-'0';
					i++;
				}
				if(cnt1==0&&cnt2==0&&cnt3==0) m1[s]+=1;
				else if(cnt1==0&&cnt2==0) m1[s]+=cnt3;
				else if(cnt2==0&&cnt3==0) m1[s]+=cnt1;
				else if(cnt1==0&&cnt3==0) m1[s]+=cnt2;
				else if(cnt1==0) m1[s]+=cnt2*cnt3;
				else if(cnt2==0) m1[s]+=cnt1*cnt3;
				else if(cnt3==0) m1[s]+=cnt1*cnt2;
 				else m1[s]+=cnt1*cnt2*cnt3;
 				//cout<<i<<' '<<s<<endl;
				//cout<<s<<' '<<cnt1<<' '<<cnt2<<' '<<cnt3<<endl;
				if(s.length()>0) 
					v.push_back(s);
				if(str[i]=='+'||str[i]=='=') break;
				
			}
			i++;
		}
		//cout<<i<<endl;
		while(i<str.length())
		{
			//cout<<i<<endl;
			if(str[i-1]=='+'||str[i-1]=='=')//到系数之前初始化 
			{
				cnt1=0;
			}
			while(str[i]>='0'&&str[i]<='9')//取系数 
			{
				cnt1*=10;
				cnt1+=str[i]-'0';
				i++;
			}
			int cnt3=0,jmp;
			while(1)//取每个元素及其下标 
			{
				
				cnt2=0;
				string s;
				if(str[i]=='(')//取括号外数字 
				{
					cnt3=find(i);
					i++;
				}
				if(str[i]>='A'&&str[i]<='Z')//取元素 
				{
					s+=str[i];
					if(str[i+1]>='a'&&str[i+1]<='z')
					{
						s+=str[i+1];
						i++;
					}
				}
				i++;
				while(str[i]>='0'&&str[i]<='9')
				{
					cnt2*=10;
					cnt2+=str[i]-'0';
					i++;
				}
				if(cnt1==0&&cnt2==0&&cnt3==0) m2[s]+=1;
				else if(cnt1==0&&cnt2==0) m2[s]+=cnt3;
				else if(cnt2==0&&cnt3==0) m2[s]+=cnt1;
				else if(cnt1==0&&cnt3==0) m2[s]+=cnt2;
				else if(cnt1==0) m2[s]+=cnt2*cnt3;
				else if(cnt2==0) m2[s]+=cnt1*cnt3;
				else if(cnt3==0) m2[s]+=cnt1*cnt2;
 				else m2[s]+=cnt1*cnt2*cnt3;
 				//cout<<i<<' '<<s<<endl;
				//cout<<s<<' '<<s.length()<<' '<<cnt1<<' '<<cnt2<<' '<<cnt3<<endl;
				if(s.length()>0) 
					v.push_back(s);
				if(str[i]=='+'||i>=str.length()) break;	
			}
			i++;
		}
		bool flag=0;
		for(int i=0;i<v.size();i++)
		{
			string s=v[i];
			//cout<<s<<' '<<m1[s]<<' '<<m2[s]<<endl;
			if(m1[s]!=m2[s])
			{
				flag=1;
				cout<<"N"<<endl;
				break;
			}
		}
		if(!flag) cout<<"Y"<<endl;
	} 
	return 0;
} 
/*
1
(NH4)(Cl)+(NH4)(Cl)=2(NH4)(Cl)
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值