萌新问问C语言,作业不会做

 


#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, D, x1, x2;
    printf("请输入a、b、c的值:");
    scanf("%f %f %f", &a, &b, &c);
    D = b * b - 4 * a * c;
    if (D > 0) {
        x1 = (-b + sqrt(D)) / (2 * a);
        x2 = (-b - sqrt(D)) / (2 * a);
        printf("方程有两个实根:x1=%.2f,x2=%.2f\n", x1, x2);
    } else if (D == 0) {
        x1 = -b / (2 * a);
        printf("方程有一个实根:x=%.2f\n", x1);
    } else {
        printf("方程有两个虚根\n");
    }
    return 0;
}

我们需要计算判别式D=b^2-4ac。如果D>0,则方程有两个实根;如果D=0,则方程有一个实根;如果D<0,则方程有两个虚根。然后,我们可以使用以下公式计算根:x1=(-b+sqrt(D))/(2a)和x2=(-b-sqrt(D))/(2a),我们需要包含math.h头文件以使用sqrt函数。望采纳!