首页 > 试题广场 >

计算一元二次方程

[编程题]计算一元二次方程
  • 热度指数:87341 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
从键盘输入a, b, c的值,编程计算并输出一元二次方程ax2 + bx + c = 0的根,当a = 0时,输出“Not quadratic equation”,当a ≠ 0时,根据△ = b2 - 4*a*c的三种情况计算并输出方程的根。

输入描述:
多组输入,一行,包含三个浮点数a, b, c,以一个空格分隔,表示一元二次方程ax2 + bx + c = 0的系数。


输出描述:

针对每组输入,输出一行,输出一元二次方程ax2 + bx +c = 0的根的情况。

  如果a = 0,输出“Not quadratic equation”;

  如果a ≠  0,分三种情况:

△ = 0,则两个实根相等,输出形式为:x1=x2=...

△  > 0,则两个实根不等,输出形式为:x1=...;x2=...,其中x1  <=  x2。

△  < 0,则有两个虚根,则输出:x1=实部-虚部i;x2=实部+虚部i,即x1的虚部系数小于等于x2的虚部系数,实部为0时不可省略。实部= -b / (2*a),虚部= sqrt(-△ ) / (2*a)

所有实数部分要求精确到小数点后2位,数字、符号之间没有空格。

示例1

输入

2.0 7.0 1.0

输出

x1=-3.35;x2=-0.15
示例2

输入

0.0 3.0 3.0

输出

Not quadratic equation
示例3

输入

1 2 1

输出

x1=x2=-1.00
示例4

输入

2 2 5

输出

x1=-0.50-1.50i;x2=-0.50+1.50i
示例5

输入

1 0 1

输出

x1=0.00-1.00i;x2=0.00+1.00i
#include <stdio.h>
#include <math.h>
int main() {
    float a,b,c;
    double m,n,t,x1,x2,q,p;
    while(scanf("%f %f %f",&a,&b,&c)!=EOF){
   
    m=b*b-4*a*c;
    n=sqrt(m);
    if(a==0)
    printf("Not quadratic equation\n");
    else{
        if(m==0){
            t=-b/(2*a);
            if(t==-0.0){
                t=0.00;
            printf("x1=x2=%.2lf\n",t);
            }
            else
             printf("x1=x2=%.2lf\n",t);;
        }
        if(m>0){
            x1=(-b+n)/(2*a);
            x2=(-b-n)/(2*a);
            if(x1>x2){
                double temp=x1;
                x1=x2;
                x2=temp;
            }
            printf("x1=%.2lf;x2=%.2lf\n",x1,x2);
        }
        if(m<0){
            p=-b/(2*a);
            q=sqrt(-m)/(2*a);
            printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n",p,q,p,q);
        }
    }}

    return 0;
    }
细节挺多的
发表于 2024-11-26 14:10:49 回复(0)

#include<stdio.h>

#include<string.h> #include<math.h> int main() {     float a,b,c,result;     double dlt;    while((scanf("%f %f %f",&a,&b,&c))!=EOF)    {     dlt=b*b-4*a*c;     if(a==0)     printf("Not quadratic equation\n");     else     {     if(dlt==0){         float fenmu=-b+(sqrt(dlt));         if(fenmu==0)         printf("x1=x2=%.2f\n",fenmu);         else         printf("x1=x2=%.2f\n",(-b+sqrt(dlt))/2.0/a);     }     else if(dlt>0)     {         printf("x1=%.2f;x2=%.2f\n",(-b-(float)sqrt(dlt))/(2.0*a),(-b+(float)sqrt(dlt))/(2.0*a));     }     else if(dlt<0)     {   if(dlt<0)             dlt=-dlt;             float xu=(sqrt(dlt))/2.0/a;             if(xu<0)             xu=-xu;             if(b==0)             b=-b;         printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",-b/2.0/a,xu,-b/2/a,xu);     }     }     }     return 0; }

发表于 2024-11-01 00:02:03 回复(1)
#include<stdio.h>
#include<math.h>
int main() {
    float a = 0, b = 0, c = 0;
    //scanf("%f %f %f", &a, &b, &c);
    while (scanf("%f %f %f", &a, &b, &c) != EOF) {
        float d = b * b - 4 * a * c; //判别式
        float x1 = 0, x2 = 0; //根
        if (a == 0) {
            printf("Not quadratic equation");
        } else {
            if (d == 0) {
                x1 = x2 = (-b / (2 * a));
                if (x2 == 0) {
                    printf("x1=x2=0.00\n");
                } else {
                    printf("x1=x2=%.2f\n", x1);
                }
            } else if (d < 0) {
                //x1 = -b/2*a - sqrt(-d) / (2 * a);
                //x2 = -b/2*a + sqrt(-d) / (2 * a);
                float e = -b / (2 * a);
                float f = sqrt(-d) / (2 * a);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", e, f, e, f);
            } else if (d > 0) {
                x1 = (-b - sqrt(d)) / (2 * a);
                x2 = (-b + sqrt(d)) / (2 * a);
                printf("x1=%.2f;x2=%.2f\n", x1, x2);
            }
        }
    }
    return 0;
}
发表于 2024-10-30 21:24:38 回复(0)
#include<stdio.h>
#include<math.h>

void compare(float *x ,float *y)
{
if(*x>*y)
{
float ling=*x;
*x=*y;
*y=ling;
}
}//保证小在前
int main()
{
float a=0.0f;
float b=0.0f;
float c=0.0f;
float x1=0.0f,x2=0.0f;

while((scanf("%f %f %f",&a,&b,&c))!=EOF)
 {
if(a==0)
printf("Not quadratic equation\n");
else
{
    if((b*b-4*a*c)>0)
{
x1=(-b-sqrt(b*b-4*a*c))/2/a;
x2=(-b+sqrt(b*b-4*a*c))/2/a;
compare(&x1,&x2);
printf("x1=%.2f;x2=%.2f\n",x1,x2);
}
    else if((b*b-4*a*c)==0)
       printf("x1=x2=%.2f\n",(-b)/2/a+0);
        else
        {
              x1=(-sqrt(4*a*c-b*b))/2/a;
              x2=(sqrt(4*a*c-b*b))/2/a;
              compare(&x1,&x2);
printf("x1=%.2f%.2fi;x2=%.2f+%.2fi\n",(-b)/2/a,x1,(-b)/2/a,x2);
     
        }

}
}
    return 0;
}
发表于 2024-08-02 17:47:18 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>

int main() {
    float a, b, c;
    while (scanf("%f %f %f", &a, &b, &c) != EOF) {
        if (a == 0.0)printf("Not quadratic equation\n");
        else {
            float d = b * b - 4 * a * c;
            if (d == 0) {
                float x = (-b + sqrt(d)) / (2 * a);
                printf("x1=x2=%.2f\n", x + 0);
                //看评论区才知道-0.0+0可以消去负号 确实妙
                //自己原计划是用if直接输出 如下
                //if (x == -0.0)printf("x1=x2=0.00");
            }
            else if (d > 0) {
                float x1 = (-b + sqrt(d)) / (2 * a);
                float x2 = (-b - sqrt(d)) / (2 * a);
                printf("x1=%.2f;x2=%.2f\n", x1<x2 ? x1 : x2, x1>x2 ? x1 : x2);
            }
            else if (d < 0) {
                float s = (-b) / (2 * a);
                float x = sqrt(-d) / (2 * a);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", s, x, s, x);
            }
        }
    }

    return 0;
}

发表于 2024-08-02 11:32:36 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    double a, b, c;
    while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) {
        if (a == 0) {
            printf("Not quadratic equation\n");
        } else {
            double discriminant = b * b - 4 * a * c;
            if (discriminant == 0) {
                double root = -b / (2 * a);
                if (root == -0.0) root = 0.0; // 处理 -0.0 的情况
                printf("x1=x2=%.2lf\n", root);
            } else if (discriminant > 0) {
                double root1 = (-b - sqrt(discriminant)) / (2 * a);
                double root2 = (-b + sqrt(discriminant)) / (2 * a);
                if (root1 == -0.0) root1 = 0.0; // 处理 -0.0 的情况
                if (root2 == -0.0) root2 = 0.0; // 处理 -0.0 的情况
                printf("x1=%.2lf;x2=%.2lf\n", root1, root2);
            } else {
                double realPart = -b / (2 * a);
                double imaginaryPart = sqrt(-discriminant) / (2 * a);
                if (realPart == -0.0) realPart = 0.0; // 处理 -0.0 的情况
                printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", realPart, imaginaryPart,
                       realPart, imaginaryPart);
            }
        }
    }
    return 0;
}

发表于 2024-06-30 20:42:02 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    float a = 0, b = 0, c = 0;
    float x1 = 0, x2 = 0;
    while (scanf("%f %f %f", &a, &b, &c) != EOF)
    {
        if (a == 0)
        {
            printf("Not quadratic equation\n");
       
        }
        else
        {
            float ret = 0, g = 0;
            ret = b * b - 4 * a * c;

            if (ret >= 0)
            {
                g = sqrt(ret);

                x1 = (-b + g) / (2.0 * a);
                x2 = (-b - g) / (2.0 * a);

                if (x1 == x2 || (x1 == -0 && x2 == 0) || (x1 == 0 && x2 == -0))
                {
                    if ((x1 == x2) && (x1 == 0 || x2 == 0))
                    {
                        printf("x1=x2=0.00\n");
                        continue;
                    }

                    printf("x1=x2=%.2f\n", x2);
                    continue;
                }

                if(x1>x2)
                {float temp=x2; x2=x1; x1=temp;}
                   

                { printf("x1=%.2f;x2=%.2f\n", x1, x2); }


            }
            else if (ret < 0)
            {

                float s = -b / (2 * a);
                float f = sqrt(-ret) / (2 * a);
                x2 = -b / (2 * a) + sqrt(-ret) / (2 * a);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", s, f, s, f);
               
            }

        }

    }
    return 0;
}
发表于 2024-04-30 08:58:15 回复(0)
#include <stdio.h>
#include <math.h>

int main()
{
    //因子
	float a = 0.0f;
	float b = 0.0f;
	float c = 0.0f;
	//判别式
    float disc = 0.0f;
    //实数根
    float root1 = 0.0f;
	float root2 = 0.0f;
	//共轭复数的实部与虚部
    float real = 0.0f;
	float imag = 0.0f;

    //录入数据
    while(scanf("%f%f%f", &a, &b, &c) == 3)
    {
        //判断是否为一元二次方程
        if(a == 0.0)
        {
            printf("Not quadratic equation\n");
            continue;
        }
        
        //求根公式的判别式
        disc = b * b - 4 * a * c;
        
        //进入求根流程
        if (disc <= 1e-6 && disc >= -1e-6)//有2个相同实数根
        {
            root1 = (-b + sqrt(disc)) / (2 * a);

            //消除-0.0的情况
            if(root1 <= 1e-6 && root1 >= -1e-6)
            {
                root1 = root2 = 0.0; 
            }

            printf("x1=x2=%.2f\n", root1);
        }
        else if (disc > 0.0)//有2个不同实数根
        {
            root1 = (-b - sqrt(disc)) / 2 / a;
            root2 = (-b + sqrt(disc)) / 2 / a;
            
            printf("x1=%.2f;x2=%.2f\n", root1, root2);
        }
        else//有2个共轭复数根
        {
            real = -b / 2 / a;
            imag = sqrt(-disc) / 2 / a;

            printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", real, imag, real, imag);
        }
    }

	return 0;
}

发表于 2024-04-26 16:56:03 回复(0)
#include <stdio.h>

#include<math.h>    //会用到sqrt
void check_equation(double a, double b, double c)   //求根函数
 {
    double x1=0,x2=0;  //两个根
    if(a == 0) 
    {
        printf("Not quadratic equation\n");
    }
    else    //a不为0的情况
    {
        double M = b*b-(4*a*c); //后面不用计算,提升效率,判断式
       

        if(M==0)        //两根相等
        {
            double x = b/(-2*a); //相等时的求根公式
            if(x == 0)      //x等于0.00时总是以-0.00显示
            printf("x1=x2=0.00\n");
            else
            printf("x1=x2=%.2lf\n", x);
        }
        else if(M>0)    //两根为实根
        {
             x1 = (-b+sqrt(M))/(2*a);    
             x2=  (-b-sqrt(M))/(2*a);    //求根公式
            if(x1<=x2)  //小的根一定在前面
            printf("x1=%.2lf;x2=%.2lf\n",x1,x2);  
            else
            printf("x1=%.2lf;x2=%.2lf\n",x2,x1);
        }
        else            //两个为虚根
        {
            double shi = -b/(2*a);          //虚部,虚部和实部不能相加
            double xu = sqrt(-M)/(2*a);    //一定是正数!!!
            //直接打印
            //因为虚部显示是相反数,而xu一定为正数
            //所以x1 一定是“-”减号字符,x2一定是“+”加号字符
            printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi",shi,xu,shi,xu);

            // double xu2 = -xu1;           //不用比较虚部的大小了
            // if(xu1<=xu2) 
            // {
            //     printf("x1=%.2lfi;x2=%.2lfi\n",);
            // }
            // else
            //     printf("x1=%.2lfi;x2=%.2lfi\n",x4,x3);
        }
    }
 }

int main() 
{
    double a,b,c;
    while(scanf("%lf %lf %lf", &a, &b, &c) !=EOF)   //如果在成功读取任何数据之前发生任何情况
                                                    //则返回 EOF。
    {
        check_equation(a,b,c);   //求根函数
    }
    return 0;
}

编辑于 2024-04-18 23:07:19 回复(0)
编辑于 2024-03-26 15:26:21 回复(0)
#include <math.h>
#include <stdio.h>

int main() {
    float a, b, c;
    while (scanf("%f %f %f", &a, &b, &c) != EOF) {
        float delta = pow(b, 2) - 4 * a * c;
        if (a == 0) {
            printf("Not quadratic equation\n");
        }
        if (a != 0) {
            if (delta == 0) {
                printf("x1=x2=%.2f\n", (-b) / (2.0 * a) + 0);
            }
            if (delta > 0) {
                printf("x1=%.2f;x2=%.2f\n", (-b - sqrt(delta)) / (2 * a),
                       (-b + sqrt(delta)) / (2 * a));
            }
            if (delta < 0) {
                if (b != 0) {
                    float delta = pow(b, 2) - 4 * a * c;
                    printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", -b / (2 * a),
                           sqrt(-(b * b - 4 * a * c)) / (2 * a), -b / (2 * a),
                           sqrt(-(b * b - 4 * a * c)) / (2 * a));
                } else {
                    printf("x1=0.00-%.2fi;x2=0.00+%.2fi\n", sqrt(-(b * b - 4 * a * c)) / (2 * a),
                           sqrt(-(b * b - 4 * a * c)) / (2 * a));
                }

            }
        }

    }
    return 0;
}


发表于 2024-02-17 15:24:33 回复(1)
#include <stdio.h>
#include <math.h>

int main() {
    float a[3] = {0}, b = 0, c[2] = {0};
    while (scanf("%f %f %f", &a[0], &a[1], &a[2]) != EOF) {
        if (a[0] == 0) {
            printf("Not quadratic equation");
        } else {
            b = (pow(a[1], 2) - 4 * a[0] * a[2]);
            if (b == 0) {
                c[0] = (-a[1] + 0) / (2 * a[0]);
                if (c[0] == 0) {
                    printf("x1=x2=0.00");
                } else {
                    printf("x1=x2=%.2f\n", c[0]);
                }
                //printf("%f", c[0]);
                //printf("x1=x2=%.2f", c[0]);
            }
            if (b > 0) {
                c[0] = (-a[1] - sqrt(b)) / (2 * a[0]);
                c[1] = (-a[1] + sqrt(b)) / (2 * a[0]);
                printf("x1=%.2f;x2=%.2f", c[0], c[1]);
            }
            if (b < 0) {
                c[0] = -a[1] / (2 * a[0]);
                c[1] = (sqrt(-b) / (2 * a[0]));
                //printf("%.2f %.2f", c[0], c[1]);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi", c[0], c[1], c[0], c[1]);
            }
        }
    }
    return 0;
}
发表于 2024-01-18 21:59:07 回复(0)
#include <math.h>
#include <stdio.h>

int main() {
    float a,b,c;
    while(scanf("%f %f %f",&a,&b,&c)!=EOF){
    if(a == 0)
    printf("Not quadratic equation");
    else{
        float dert =(b*b-4*a*c);
        float m = (-1)*b/(2*a);
        if(dert==0&&b!=0)
        printf("x1=x2=%.2f\n",m);
        else if(dert==0&&b==0)
        printf("x1=x2=0.00\n");
        else if(dert < 0){
        dert = sqrt(-dert)/(2*a);
        printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",m,dert,m,dert);
        }
        else if(dert > 0){
            dert = sqrt(dert)/(2*a);
            printf("x1=%.2f;x2=%.2f\n",m-dert,m+dert);
        }
    }
    }
    return 0;
}
发表于 2023-10-21 23:08:46 回复(0)
#include <stdio.h>
#include <math.h>
int main()
{
    double a =0.0;
    double  b =0.0;
    double  c =0.0;
while (scanf("%lf %lf %lf", &a, &b, &c) != EOF)
{
    if (a == 0)
    {
        printf("Not quadratic equation\n");
    }
    else
    {
        double t = b * b - 4.0 * a * c;
        if (t == 0)
        {
            if (b == 0)
            {
                printf("x1=x2=0.00\n");
            }
            else
            {
                printf("x1=x2=%.2lf\n", (-b + t) / (2.0 * a));
            }
        }
        else if (t > 0)
        {
            double  p = (-b + sqrt(t)) / (2.0 * a);
            double q = (-b - sqrt(t)) / (2.0 * a);
            if (p > q)
            {
                printf("x1=%.2lf;x2=%.2lf\n", q, p);
            }
            else
            {
                printf("x1=%.2lf;x2=%.2lf\n", p, q);
            }
        }
        else if (t < 0)
        {
            double  x1 = -b / (2 * a);
            double  x2 = ((sqrt(-t)) / (2 * a));
            printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi", x1, x2, x1, x2);
        }
    }
}

    return 0;
}
发表于 2023-08-02 19:53:07 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, d;
    float x1, x2;
    while (scanf("%f %f %f", &a, &b, &c) != EOF) {
        if (0 == a) {
            printf("%s\n", "Not quadratic equation");
        } else {
            d = (b * b) - (4 * a * c);
            if (d == 0) {
                x1 = (-b) / (2 * a);
                if (x1 == -0) {
                    x1=0;
                    printf("x1=x2=%.2f\n", x1);
                } else {
                    printf("x1=x2=%.2f\n", x1);
                }
            } else if (d > 0) {
                x1 = ((-b) - sqrt(d)) / (2 * a);
                x2 = ((-b) + sqrt(d)) / (2 * a);
                printf("x1=%.2f;x2=%.2f\n", x1, x2);
            } else {
                x1 = -b / (2 * a);
                x2 = sqrt(-d) / (2 * a);
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", x1, x2, x1, x2);
            }
        }
    }
    return 0;
}

发表于 2023-03-09 19:18:26 回复(0)
#include <stdio.h>
#include <math.h>
int main() {
    float a = 0;
    float b = 0;
    float c = 0;
    float x1 = 0;
    float x2 = 0;
    while(scanf("%f %f %f", &a, &b, &c) != EOF)
    {
        if (a == 0)
        {
            printf("Not quadratic equation\n");
        }
        else
        {
            int d = b * b - 4.0 * a * c;
            if (d == 0)
            {
                x1 = -b / (2.0 * a);
                if(b == 0)
                {
                    printf("x1=x2=0.00\n");
                }
                else {
                    printf("x1=x2=%.2f\n", x1);
                }  
            }
            else if (d > 0)
            {
                x1 = (-b + sqrt(d)) / (2.0 * a);
                x2 = (-b - sqrt(d)) / (2.0 * a);
                printf("x1=%.2f;x2=%.2f\n", x1 < x2 ? x1 : x2, x1 > x2 ? x1 : x2);
            }
            else
            {
                x1 = (-b) / (2.0 * a);
                x2 = sqrt(-d) / (2.0 * a);
                if(b == 0)
                {
                    printf("x1=0.00-%.2fi;x2=0.00+%.2fi\n", x2, x2);
                }
                else
                {
                    printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", x1, x2, x1, x2);
                }  
            }
        }
    }

    return 0;
}
发表于 2023-02-27 00:24:31 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    double a, b, c, root, x1, x2;
    while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) {
        getchar();
        if (a == 0) {
            printf("Not quadratic equation\n");
        } else if (a != 0) {
            root = pow(b, 2) - (4 * a * c);
            // 两个根相等
            if (root == 0) {
                // 求根公式
                x1 = ((-b) + sqrt(root)) / (2 * a);
                // 将-0.00变为0
                x1 == 0 ? x1 = 0 : x1;
                printf("x1=x2=%.2lf\n", x1);
            }
            // 两个根不等
            else if (root > 0) {
                // 求根公式
                double temp;
                x1 = ((-b) + sqrt(root)) / (2 * a);
                x2 = ((-b) - sqrt(root)) / (2 * a);
                // 检测x1 <= x2?
                x1 <= x2 ?
                printf("x1=%.2lf;x2=%.2lf\n", x1, x2) :
                printf("x1=%.2lf;x2=%.2lf\n", x2, x1);
            }
            // 两个虚根
            else if (root < 0) {
                // 求虚根1的实部x1,虚部x2
                x1 = (-b) / (2 * a);
                x2 = sqrt(-root) / (2 * a);
                printf("x1=%.2lf-%.2lfi;", x1, x2);
                // 求虚根2的实部x1,虚部x2
                x1 = (-b) / (2 * a);
                x2 = sqrt(-root) / (2 * a);
                printf("x2=%.2lf+%.2lfi\n", x1, x2);
            }

        }
    }

    return 0;
}

发表于 2023-02-15 16:46:34 回复(0)