解题思路:
注意事项:
保留三位小数
参考代码:
#include <iostream> #include <cmath> #include <iomanip> using namespace std; void func (int a, int b, int c) { int d = b * b - 4 * a * c; if (d >= 0) { double x1 = ( -b + sqrt(d) ) / (2 * a * 1.0), x2 = ( -b - sqrt(d) ) / (2 * a * 1.0); cout << fixed << setprecision(3) << "x1=" << x1 << ' ' << "x2=" << x2; } else { double p = -b / (2 * a * 1.0), q = sqrt(-d) / (2 * a * 1.0); cout << fixed << setprecision(3) << "x1=" << p << "+" << q << "i" << ' ' << "x2=" << p << "-" << q << "i" ; } } int main() { int a = 0, b = 0, c = 0; cin >> a >> b >> c; func( a, b, c ); return 0; }
0.0分
0 人评分