题解 | 计算一元二次方程
计算一元二次方程
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a, b, c;
float x1,x2;
while (cin >> a >> b >> c)
{ // 注意 while 处理多个 case
float deta = b *b -4*a*c;
if(a == 0) cout << "Not quadratic equation" << endl;
else if(deta == 0)
{
float mp = -b + sqrt(deta);
if(mp == 0)
{
printf("x1=x2=%.2f\n",mp);
}
else{
printf("x1=x2=%.2f\n",(-b+sqrt(deta))/2.0/a);
}
}
else if(deta > 0)printf("x1=%.2f;x2=%.2f\n",(-b-sqrt(deta))/2.0/a,(-b+sqrt(deta))/2.0/a);
else if(deta < 0) printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",-b/2/a,sqrt(-deta)/2/a,-b/2/a,sqrt(-deta)/2/a);
}
}
// 64 位输出请用 printf("%lld")
为什么会有输出为-0.00的情况
腾讯成长空间 2536人发布