1 条题解

  • 0
    @ 2025-11-23 15:22:51

    QAQ

    #include<bits/stdc++.h>
    using namespace std;
    vector<int>lx,rx,ld,rd;
    int main()
    {
    	string str;
    	cin>>str;
    	int x=0,f=1;
    	bool flag=false;
    	string a="";
    	bool leap=true;
    	bool go=false;
    	for(int i=0;i<str.size();i++){
    		if(str[i]>='a'&&str[i]<='z'){
    			if(go==true){
    				continue;
    			}
    			go=true;
    			if(i==0||!isdigit(str[i-1])){
    				str.insert(i,"1");
    			}
    		}else{
    			go=false;
    		}
    	}
    	int p=0;
    	for(int i=0;i<str.size();i++){
    		if(str[i]=='='){
    			p=i+1;
    			if(flag==true){
    				lx.push_back(x*f);
    			}else{
    				ld.push_back(x*f);
    			}
    			break;
    		}
    		if(str[i]>='a'&&str[i]<='z'){
    			flag=true;
    			if(!a.empty()){
    				continue;
    			}
    			if(leap==true){
    				a.push_back(str[i]);
    			}
    			continue;
    		}else{
    			if(!a.empty()){
    				leap=false;
    			}
    		}if(isdigit(str[i])){
    			x=x*10+(str[i]-'0');
    		}else{
    			if(flag==true){
    				lx.push_back(x*f);
    			}else{
    				ld.push_back(x*f);
    			}
    			if(str[i]=='+'){
    				f=1;
    			}else if(str[i]=='-'){
    				f=-1;
    			}
    			flag=false;
    			x=0;
    		}
    	}
    	x=0;
    	f=1;
    	str.push_back(' ');
    	flag=go=false;
    	leap=true;
    	for(int i=p;i<str.size();i++){
    		if(str[i]>='a'&&str[i]<='z'){
    			flag=true;
    			if(!a.empty()){
    				continue;
    			}
    			if(leap==true){
    				a.push_back(str[i]);
    			}
    			continue;
    		}else{
    			if(!a.empty()){
    				leap=false;
    			}
    		}if(isdigit(str[i])){
    			x=x*10+(str[i]-'0');
    		}else{
    			if(flag==true){
    				rx.push_back(x*f);
    			}else{
    				rd.push_back(x*f);
    			}
    			if(str[i]=='+'){
    				f=1;
    			}else if(str[i]=='-'){
    				f=-1;
    			}
    			flag=false;
    			x=0;
    		}
    	}
    	int ans=0;
    	for(auto x:ld){
    		ans+=x;
    	}
    	for(auto x:rd){
    		ans-=x;
    	}
    	int yy=0;
    	for(auto x:lx){
    		yy-=x;
    	}
    	for(auto x:rx){
    		yy+=x;
    	}
    	cout<<a<<"="<<fixed<<setprecision(3)<<ans*1.0/yy;
    	return 0;
    }
    
    

    [NOIP 2000 普及组] 计算器的改良

    信息

    ID
    22
    时间
    1000ms
    内存
    125MiB
    难度
    5
    标签
    递交数
    1
    已通过
    1
    上传者