#include<bits/stdc++.h>
using namespace std;
char c;
int now1,cnt1,xs1;
int now2,cnt2,xs2;
bool k;
int p = 1;
char word;
int main(){
c = getchar();
while(c!='='){
if(isdigit(c)) now1 = now1*10+c-'0';
else if(c=='-'){
now1 *= p;
cnt1 += now1;
p = -1,now1 = 0;
}
else if(c=='+'){
now1 *= p;
cnt1 += now1;
p = 1,now1 = 0;
}
else{
if(!k){
word = c;
k = 1;
}
if(now1) xs1 += now1*p;
else{
xs1 += p;
}
now1 = 0;
}
c = getchar();
}
cnt1 += now1*p;
p = 1;
c = getchar();
while(c!='\n'){
if(isdigit(c)) now2 = now2*10+c-'0';
else if(c=='-'){
now2 *= p;
cnt2 += now2;
p = -1,now2 = 0;
}
else if(c=='+'){
now2 *= p;
cnt2 += now2;
p = 1,now2 = 0;
}
else{
if(!k){
word = c;
k = 1;
}
if(now2) xs2 += now2*p;
else xs2 += p;
now2 = 0;
}
c = getchar();
}
cnt2 += now2*p;
double a = cnt2 - cnt1;
double b = xs1 - xs2;
double m = a/b;
if(abs(m)<1e-8) m = 0;
printf("%c=%.3lf",word,m);
return 0;
}
这是我的代码(马蜂丑,抱歉
全 WA 了之后下载了这组(第一个测试点)数据:
in:
20+3x=-18
out:
x=-12.667
上面的代码在本地 DEV 中运行这组数据是正确的,然而在洛谷在线 IDE 中运行却会输出
x=-0.952

因此我即使在 DEV 中运行是正确的,但是交到洛谷上 #1 仍然 WA。
我就不大理解。
这不大可能是 Windows 系统与 Linux 系统的差异吧?
也没有从自己的代码中找出未定义行为(UB)。
烦请大家帮我看一下代码,或是从自己的本地 IDE(DEV,VSCode 等都行)运行一下看看是否正确,还是我的 DEV 有问题。