1 条题解

  • 0
    @ 2025-4-12 22:03:05

    C :

    #include<stdio.h>
    #include<math.h>
    int main()
    {
    	double a, b, c, C;
    	scanf("%lf%lf%lf", &a, &b, &c);
    	C = pow(b, 2) - 4 * a * c;
    	if(C == 0)
    		printf("x1=%.3lf x2=%.3lf\n", -b / (2 * a), -b / (2 * a));
    	if(C < 0)
    		printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi\n",-b / (2 * a), sqrt(-C) / (2 * a), -b / (2 * a), sqrt(-C) / (2 * a));
    	if(C > 0)
    	printf("x1=%.3lf+%.3lf x2=%.3lf-%.3lf\n",-b / (2 * a), sqrt(C) / (2 * a), -b / (2 * a), sqrt(C) / (2 * a));
    }
    

    C++ :

    #include <stdio.h>
    #include <cmath>
    #include <string.h>
    #include <cstdlib>
    #include <iostream>
    #define eps 1e-6
    using namespace std;
    
    
    int main()
    {
        double a, b, c, disc, x1, x2, real, imag;
        scanf("%lf%lf%lf", &a, &b, &c);
    
        disc = b * b - 4 * a * c;
        if(fabs(disc) <= eps)
        {
            printf("x1=%.3lf ", -b / (2 * a));
            printf("x2=%.3lf\n", -b / (2 * a));
        }
        else if(fabs(disc > eps))
        {
            x1 = (-b + sqrt(disc)) / (2 * a);
            x2 = (-b - sqrt(disc)) / (2 * a);
            printf("x1=%.3lf x2=%.3f\n", x1, x2);
        }
        else
        {
            real = -b / (2 * a);
            imag = sqrt(-disc) / (2 * a);
    
            printf("x1=%.3f+%.3fi ", real, imag);
            printf("x2=%.3f-%.3fi\n", real, imag);
        }
    
        return 0;
    }
    
    
    • 1

    C语言程序设计教程(第三版)课后习题8.2

    信息

    ID
    2714
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者