解题思路:蛮简单的,顺着解下去就好。
注意事项:根据样例输出可知,需要保留两位数字,那么就需要设为double类型,头文件加上#include<iomanip>//要记住;
结尾输出时cout<<fixed<<setprecision(2);记得x1,x2之间加个空格键;另外注意x1,x2大小。
参考代码:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double x1, x2, a, b, c,d,t;
cin >> a >> b >> c;
d = b * b - 4 * a * c;
if (d > 0 || d < 0)
{
x1 = (-b + sqrt(d)) / 2 / a;
x2 = (-b - sqrt(d)) / 2 / a;
}
else if (d ==0)
x1 = x2 = -b / 2 / a;
if (x1 < x2)
{
t = x1;
x1 = x2;
x2 = t;
}
cout << fixed<<setprecision(2)<<x1 <<" "<< x2 << endl;
return 0;
}
大家不要吝啬给个五星吧,对我很重要,谢谢啦!
0.0分
8 人评分