因为用scanf会读到空格之类的字符,cin会忽略
by qqvq @ 2019-01-07 18:30:02
@[qqvq](/space/show?uid=29089)
那为什么我的cin会无法ac?
其他代码都一样,把cin>>c换成
~scanf()就可以ac了
by fz风 @ 2019-01-10 10:31:10
@[fz风](/space/show?uid=44614)
请发代码谢谢
by qqvq @ 2019-01-10 21:25:51
@[qqvq](/space/show?uid=29089)
我觉得是两者的区别 ,和代码没有关系
```cpp
#include<bits/stdc++.h>
#include <iostream>
#include<vector>
#include<string>
#include <iomanip>
using namespace std;
int l=0, r=0;
char x;
void solve() {
char c;
int f1 = 1, f2 = 1, tmp = 0;
while(~scanf("%c",&c)){
if (c >= '0' and c <= '9') {
tmp *= 10;
tmp += c - '0';
}
else {
if (c >= 'a'&&c <= 'z') l += (tmp == 0) ? f2 * f1 : tmp * f2*f1, x = c;//判断系数是否为1
else r += tmp * (-f2)*f1;
tmp = 0;
}
if (c == '+') { f1 = 1; continue; }
if (c == '-') { f1 = -1; continue; }
if (c == '=') { f2 = -1; f1 = 1; continue; }
}
}
int main()
{
solve();
printf("%c=%0.3f", x, (float)r / l == 0 ? abs((float)r / l) : (float)r / l);
//system("pause");
return 0;
}
```
by fz风 @ 2019-01-11 18:22:45
@[fz风](/space/show?uid=44614)
原因是scanf读入了换行符 “恰好”让你的程序对了(我觉得不会有人有心利用这两个字符)
在solve最后加入
```cpp
r += tmp * (-f2) * f1;
```
就都可以了
这还是你的代码有问题
by qqvq @ 2019-01-11 21:01:40
@[fz风](/space/show?uid=44614)
你的代码怎么和“_风休住_ ”的题解那么像啊?
by Mitch谜团 @ 2019-02-08 09:50:04
哦我知道了,你也看了他/她的的题解。
by Mitch谜团 @ 2019-02-08 10:01:07