材料内容是:
给定实数 a, b, c 。 研究双二次方程 ax4 + bx2 + c = 0,即找到给定方程的所有实根并表示出来,如果没有请写出
我写了一半发现我的代码不对劲,改了三个小时,不清楚是哪的问题
cout << "ax^4+bx^2+c=0" << endl;
float x1,x2,x3,x4, a, b, c, discriminant;
cout << "a:"; cin >> a;
cout << endl;
cout << "b:"; cin >> b;
cout << endl;
cout << "c:"; cin >> c;
cout << endl;
discriminant = b * b - 4 * a * c;
x1 = sqrt((-b + sqrt(discriminant)) / (2 * a));
x2=-sqrt((-b + sqrt(discriminant)) / (2 * a));
x3= sqrt((-b - sqrt(discriminant)) / (2 * a));
x4=-sqrt((-b - sqrt(discriminant)) / (2 * a));
cout << "x1=" << x1 << endl;
cout << "x2=" << x2 << endl;
cout << "x3=" << x3 << endl;
cout << "x4=" << x4 << endl;
#include <iostream>
int main()
{
double a, b, c, d;
std::cin >> a >> b >> c;
d = b * b - 4 * a * c;
if (d < 0)
{
std::cout << "no real solution\n";
return 0;
}
d = std::sqrt(d);
int n = 0;
if (-b - d >= 0)
{
double x = std::sqrt((-b - d) / (2 * a));
std::cout << x << " " << -x << " ";
n += 2;
}
if (-b + d >= 0)
{
double x = std::sqrt((-b + d) / (2 * a));
std::cout << x << " " << -x << " ";
n += 2;
}
if (n == 0)
std::cout << "no real solution\n";
return 0;
}