求方程ax2+bx+c=0的实数根。a, b, c由键盘输入, a!=0。若只有一个实数根(b2-4ac=0)则只输出x1,若无实数根(b2-4ac<0)则输出Error。
输入
2.5 7.5 1.0
输出
(注意等号前面后面都有一个空格)
x1 = -0.139853
x2 = -2.860147
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <cmath> using namespace std; int main() { double a , b, c; cin >> a >> b >> c; double det = b * b - 4 * a * c; if(det < 0) { printf("%s","Error\n"); }else if(det == 0 ) { printf("x1 = %6f\n",-1 * b / (2 * a)); }else { printf("x1 = %6f\n",(-1 * b + sqrt(det)) / (2 * a)); printf("x2 = %6f\n",(-1 * b - sqrt(det)) / (2 * a)); } return 0; } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼