求助
查看原帖
求助
1678479
Liu_wen_tao楼主2025/7/24 18:57

代码运行下列样例:

20+3x=-18

输出:

x=-12.667

其实这就是题目评测数据中的第一个,但不知为何,评测机却判定我的代码答案错误!
点击查看

Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string str;
	cin>>str;
	vector<int>lx,rx,ld,rd;
	int x,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;
	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;
}
2025/7/24 18:57
加载中...