[C.C++] C - C语言实验——一元二次方程Ⅰ

19 0
Honkers 昨天 17:36 来自手机 | 显示全部楼层 |阅读模式

Description

解一元二次方程ax2+bx+c=0的解。保证有解

Input

a,b,c的值。

Output

两个根X1和X2,其中X1>=X2。 结果保留两位小数。

Sample

Input

  1. 1 5 -2
复制代码

Output

  1. 0.37 -5.37
复制代码

Hint

提示:计算过程中,分母是(2*a)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. double f(double a, double b, double c){
  5. double x1;
  6. x1 = ((-b) + sqrt(b * b - 4 * a * c)) / (2 * a);
  7. return x1;
  8. }
  9. double h(double a, double b, double c){
  10. double x2;
  11. x2 = ((-b) - sqrt(b * b - 4 * a * c)) / (2 * a);
  12. return x2;
  13. }
  14. int main()
  15. {
  16. double a, b, c, x1, x2, t;
  17. scanf("%lf %lf %lf", &a, &b, &c);
  18. x1 = f(a, b, c);
  19. x2 = h(a, b, c);
  20. if(x1 < x2){
  21. t = x1;
  22. x1 = x2;
  23. x2 = t;
  24. }
  25. printf("%.2f %.2f\n", x1, x2);
  26. return 0;
  27. }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行