重要的是模拟思路(顺序)!
细品+注释理解~
这题还有一些好(e)心数据点
1:+0x,-0x。
2:结果为-0.000的时候要输出0.000.
okk!上
code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
int k,b,zf=1,x,f=1;
char c;
void solve() {
while(true){
x=0;
char ch=getchar();//一个一个读入
if(ch=='\n') break;//结束
while(ch>='0'&&ch<='9'){//常数
x=x*10+ch-'0';
ch=getchar();//继续
}
if(ch>='a'&&ch<='z'){//把x加到未知数的系数上
c=ch;//未知数
//f:等号左右 ,zf:正负,x:系数
k=k+f*zf*x;//
}
else{//把x加到常数b上
b=b+f*zf*x;
}
if(ch=='+') zf=1;
else if(ch=='-') zf=-1;
else if(ch=='=') f=-1,zf=1;
}
double sum=-b*1.00/k;
if(sum==0) printf("0.000");//恶心点特判
else printf("%c=%.3lf",c,sum);
}
signed main() {
//ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int tt=1;
//cin>>tt;
while(tt--) {
solve();
}
return 0;
}
over~