博客
关于我
计算一元二次方程
阅读量:58 次
发布时间:2019-02-25

本文共 1376 字,大约阅读时间需要 4 分钟。

计算一元二次方程的根

问题描述

编写一个程序,接受一行输入,包含三个浮点数a, b, c,表示一元二次方程ax² + bx + c = 0的系数。根据a的值判断方程的类型,并计算根的情况。

输入描述

输入是一行,包含三个浮点数a, b, c,以空格分隔。

输出描述

对于每组输入,输出一元二次方程ax² + bx + c = 0的根的情况:

  • 如果a = 0,输出“Not quadratic equation”;
  • 如果a ≠ 0,根据判别式△ = b² - 4ac的值:
    • △ = 0,输出两个相等的实根;
    • △ > 0,输出两个不等的实根;
    • △ < 0,输出两个复数根,格式为“x1=实部-虚部i;x2=实部+虚部i”。

所有实数部分精确到小数点后2位,数字和符号之间无空格。

代码实现

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

运行结果

程序运行时,输入一行三个浮点数,输出相应的方程根情况。例如,输入“1 2 3”会输出x1=1.00;x2=3.00,输入“1 -2 -3”会输出x1=1.00;x2=1.00,输入“2 4 8”会输出x1=1.00-2.00i;x2=1.00+2.00i。

转载地址:http://jko.baihongyu.com/

你可能感兴趣的文章
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>