S a m p l e Sample Sample I n p u t Input Input
6a-5+1=2-2a
S a m p l e Sample Sample O u t p u t Output Output
a=0.750
T r a i n Train Train o f of of T h o u g h t Thought Thought
一元二次方程
最后可以化成
k
x
+
b
=
0
kx + b = 0
kx+b=0
再化简可得
x
=
−
b
k
x = -\frac{b}{k}
x=−kb
所以只需要求出
b
b
b(将常数相加,等号右边的要
∗
−
1
*-1
∗−1)
和
k
k
k(将未知数的系数相加,等号右边同上)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m, l = 1, k, t = 1, tot;
char s, a;
int main()
{
while(cin>>s)
{
if(s == '+')m += t * k * l, l = 1, tot = 0, k = 0;
if(s == '-')m += t * k * l, l = -1, tot = 0, k = 0;
if(s == '=')m += t * k * l, t = -1, tot = 0, k = 0, l = 1;
if(s >= '0' && s <= '9')
{
k = k * 10 + s - '0', tot = 1;
}
if(s >= 'a' && s <= 'z')
{
a = s;
if(tot)n += t * k * l;
else n += l * t;
k = 0, tot = 1;
}
}
m += t * k * l;
double ans = double(-m * 1.0 / n);
if (ans == -0.0) ans = 0;
printf("%c=%.3f", a, ans);
return 0;
}