鱼C论坛

 找回密码
 立即注册
查看: 1790|回复: 5

[已解决]c语言编程题

[复制链接]
发表于 2021-1-6 11:16:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
描述
求一元二次方程“ax^2+bx+c=0”的根,其中a不等于0。结果要求精确到小数点后5位。

若两个实根相等,则输出形式为:“x1=x2=XXX.XXXXX”;

若两个实根不等,需满足根小者在前的原则,中间用分号隔开,输出形式为:“x1=XX.XXXXX;x2=XX.XXXXX";

若无实根输出“No answer!”。

格式
输入格式
输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程a的系数。
输出格式
输出一行,表示方程的解。 所有输出部分要求精确到小数点后5位,数字、符号之间没有空格。
我打的::不知道问题出哪了
#include<stdio.h>
#include<math.h>
int main()
{
        int a,b,c,g,x;
         double x1,x2;
        scanf("%d %d %d",&a,&b,&c);
        if(0==a*x*x+b*x+c)
        {
        g=b*b-4*a*c;
        if(g<0)
        printf("No answer");
        while(g>=0)
        {
                if(g>0)
                {
                x1=(-b+sqrt(g))/2*a;
                x2=(-b-sqrt(g))/2*a;
        }
        if(g==0)
      {
              x1=(-b)/2*a;
              x2=x1;
          }
    return(0);
}
    printf("%lf;%lf",x1,x2);
}
}
最佳答案
2021-1-6 11:43:56
本帖最后由 jackz007 于 2021-1-6 11:50 编辑
  1.          int a,b,c,g,x;
  2. . . . . . .
  3.         if(0==a*x*x+b*x+c)
复制代码

       x 未赋值就引用,这个逻辑条件怕是永远也无法满足。后面计算 x1、x2 的代码根本没有机会得到执行!
  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5.         int a , b , c , d                                                       ;
  6.         double x1 , x2                                                          ;
  7.         scanf("%d%d%d" , & a , & b , & c)                                       ;
  8.         d = b * b - 4 * a * c                                                   ;
  9.         if(d >= 0) {
  10.                 x1 = 1.0 * (- b + sqrt(d)) / (2 * a)                            ;
  11.                 x2 = 1.0 * (- b - sqrt(d)) / (2 * a)                            ;
  12.                 if(x2 < x1) printf("x1 = %.5lf ; x2 = %.5lf\n" , x2 , x1)       ;
  13.                 else if (x2 > x1) printf("x1 = %.5lf ; x2 = %.5lf\n" , x1 , x2) ;
  14.                 else printf("x1 = x2 = %.5lf\n" , x1 , x2)                      ;
  15.         } else {
  16.                 printf("No answer")                                             ;
  17.         }
  18. }
复制代码

        编译、运行实况
  1. D:\0002.Exercise\C>g++ -o x x.c

  2. D:\0002.Exercise\C>x
  3. 5 3 2
  4. No answer
  5. D:\0002.Exercise\C>x
  6. 2 5 3
  7. x1 = -1.50000 ; x2 = -1.00000

  8. D:\0002.Exercise\C>x
  9. 2 -5 3
  10. x1 = 1.00000 ; x2 = 1.50000

  11. D:\0002.Exercise\C>
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-6 11:43:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-1-6 11:50 编辑
  1.          int a,b,c,g,x;
  2. . . . . . .
  3.         if(0==a*x*x+b*x+c)
复制代码

       x 未赋值就引用,这个逻辑条件怕是永远也无法满足。后面计算 x1、x2 的代码根本没有机会得到执行!
  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5.         int a , b , c , d                                                       ;
  6.         double x1 , x2                                                          ;
  7.         scanf("%d%d%d" , & a , & b , & c)                                       ;
  8.         d = b * b - 4 * a * c                                                   ;
  9.         if(d >= 0) {
  10.                 x1 = 1.0 * (- b + sqrt(d)) / (2 * a)                            ;
  11.                 x2 = 1.0 * (- b - sqrt(d)) / (2 * a)                            ;
  12.                 if(x2 < x1) printf("x1 = %.5lf ; x2 = %.5lf\n" , x2 , x1)       ;
  13.                 else if (x2 > x1) printf("x1 = %.5lf ; x2 = %.5lf\n" , x1 , x2) ;
  14.                 else printf("x1 = x2 = %.5lf\n" , x1 , x2)                      ;
  15.         } else {
  16.                 printf("No answer")                                             ;
  17.         }
  18. }
复制代码

        编译、运行实况
  1. D:\0002.Exercise\C>g++ -o x x.c

  2. D:\0002.Exercise\C>x
  3. 5 3 2
  4. No answer
  5. D:\0002.Exercise\C>x
  6. 2 5 3
  7. x1 = -1.50000 ; x2 = -1.00000

  8. D:\0002.Exercise\C>x
  9. 2 -5 3
  10. x1 = 1.00000 ; x2 = 1.50000

  11. D:\0002.Exercise\C>
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-1-6 11:51:15 | 显示全部楼层
哇 谢谢你啊 谢谢谢谢 大佬 你qq多少呐  明天c语言考试 救救孩子吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-6 12:10:45 | 显示全部楼层
呃呃 我把x改成d d=b*b-4*a*c还是不行
#include<stdio.h>
#include<math.h>
int main()
{
        int g;
        int a,b,c;
         double x1,x2;
        scanf("%d %d %d",&a,&b,&c);
        g=b*b-4*a*c;
        if(g<0)
        printf("No answer");
        while(g>=0)
        {
                if(g>0)
                {
                x1=1.0*(-b+sqrt(g))/(2*a);
                x2=1.0*(-b-sqrt(g))/(2*a);
        }
        if(g==0)
      {
              x1=(-b)/(2*a);
              x2=x1;
          }
    return(0);
}
    printf("%.5lf;%.5lf",x1,x2);
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-6 13:11:34 | 显示全部楼层
小木研 发表于 2021-1-6 11:51
哇 谢谢你啊 谢谢谢谢 大佬 你qq多少呐  明天c语言考试 救救孩子吧

问题解决了,请给最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-1-6 13:18:10 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.         int a, b, c, delta;
  6.         double p, q, x1, x2;
  7.         printf("请输入三位数:");
  8.         scanf("%d%d%d", &a, &b, &c);

  9.         if (a < 0)
  10.         {
  11.                 a = -a;
  12.                 b = -b;
  13.                 c = -c;
  14.         }
  15.         else if (a > 0)
  16.         {
  17.                 delta = b * b - 4 * a * c;
  18.                 if (delta > 0)
  19.                 {
  20.                         p = -b / (2.0 * a);
  21.                         q = sqrt(delta) / (2.0 * a);
  22.                         x1 = p + q;
  23.                         x2 = p - q;
  24.                         printf("该方程有两个不同的解为:\nx1 = %.2f\nx2 = %.2f\n", x1, x2);
  25.                 }
  26.                 else if (delta == 0)
  27.                 {
  28.                         p = -b / (2.0 * a);
  29.                         x1 = p;
  30.                         printf("该方程有两个相同的解为:\nx1 = x2 = %.2\n", p);
  31.                 }
  32.                 else
  33.                 {
  34.                         p = -b / (2.0 * a);
  35.                         delta = -delta;
  36.                         q = sqrt(delta) / (2.0 * a);
  37.                         printf("该方程有两个共轭复根为:\nx1 = %.2f + %.2fi\nx2 = %.2f - %.2fi\n", p, q, p, q);
  38.                 }
  39.         }
  40.         else
  41.         {
  42.                 printf("该方程不是一元二次方程\n");
  43.         }
  44.         return 0;
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-3 04:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表