求 a * x^2 + b * x + c = 0 方程的根。代码用stdio.h不带任何文字表述
时间: 2023-09-04 16:12:00 浏览: 89
好的,以下是一个求解一元二次方程的 C 代码:
```c
#include <stdio.h>
#include <math.h>
int main(){double a,b,c,d,x1,x2;scanf("%lf%lf%lf",&a,&b,&c);d=b*b-4*a*c;if(d<0)puts("No real roots.");else if(d==0){x1=x2=-b/(2*a);printf("One real root: %lf.\n",x1);}else{x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);printf("Two real roots: %lf, %lf.\n",x1,x2);}return 0;}
```
其中,`a`,`b`,`c` 分别代表一元二次方程 `a * x^2 + b * x + c = 0` 中的系数,`d` 为判别式,`x1`,`x2` 分别为方程的两个根。程序会从标准输入中读入系数,然后输出方程的根。
你可以将以上代码保存为一个 `.c` 文件并编译运行。
相关问题
求 a * x^2 + b * x + c = 0 方程的根。代码用stdio.h不带文字表述
好的,以下是一个求解一元二次方程的 C 代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2;
scanf("%lf%lf%lf", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta < 0) {
printf("No real roots.\n");
} else if (delta == 0) {
x1 = x2 = -b / (2 * a);
printf("One real root: %lf.\n", x1);
} else {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("Two real roots: %lf, %lf.\n", x1, x2);
}
return 0;
}
```
其中,`a`,`b`,`c` 分别代表一元二次方程 `a * x^2 + b * x + c = 0` 中的系数,`delta` 为判别式,`x1`,`x2` 分别为方程的两个根。程序会从标准输入中读入系数,然后输出方程的根。
你可以将以上代码保存为一个 `.c` 文件并编译运行。
阅读全文
相关推荐






