- #include<iostream>
- #include<stdio.h>
- #include<math.h>
- using namespace std;
- int main()
- {
- double a, b, c, d, e, x1, x2;
- cin >> a;
- cin >> b;
- cin >> c;
- d = b*b - 4 * a*c;
- if (a == 0 && b != 0 && c != 0){//方程只有一个根
- x1 = -c / b;
- cout << "6" << endl;
- printf("%.2f",x1);
- }
- if (a == 0 && b == 0 && c == 0)cout << "5" << endl;//方程有无穷根
- if (a == 0 && b == 0 && c != 0)cout << "4" << endl;//无解
- if (d<0 && a != 0) //无实根
- {
- cout << "3" << endl;
- }
- if (d == 0 && a != 0) //两个相等实根
- {
- cout << "2" << endl;
- x1 = x2 = (-b) / (2 * a);
- printf("%.2f", x1);
- }
- if (d>0 && a != 0) //两个不相等实根
- {
- e = sqrt(d);
- x1 = (-b + e) / (2 * a);
- x2 = (-b - e) / (2 * a);
- cout << "1" << endl;
- printf("%.2f %.2f", x2,x1);
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0206201512730.html
来源: http://www.codesnippet.cn/detail/0206201512730.html