执行下面程序时,若输入1、-5、6,则输出结果为( )。 # include <stdio.h> # include <math.h> # define disc(a,b,c) b*b-4*a*c main()

admin2010-01-10  15

问题 执行下面程序时,若输入1、-5、6,则输出结果为(  )。    #  include  <stdio.h>    #  include  <math.h>    #  define  disc(a,b,c)  b*b-4*a*c    main()    {  float  x1,x2;  int  a,b,c;    scanf("%d,%d,%d",&a,&b,&c);    x1=(-b+sqrt(disc(a,b,c)))/(2*a);    x2=(-b-sqrt(disc(a,b,c)))/(2*a);    printf("%5.1f.%5.1f",x1,x2);    }

选项 A、3,2
B、3.0,2.0
C、-3,-2
D、程序出错

答案2

解析 本题是使用带参数的宏来求解一元二次方程的两个根。表达式x1=(-b+sqrt(disc(a,b,c)))/(2*a)宏展开后为: x1=(-b+sqrt(b*b-4*a*c))/(2*a)=(5+sqrt(25-24))/2=6/2=3。表达式x2=(-b-sqrt(disc(a,b,c)))/(2*a)宏展开后为: x2=(-b-sqrt(b*b-4*a*c))/(2*a)=(5-sqrt(25-24))/2=4/2=2。所以程序输出结果为3.0,2.0,应该选择B。
转载请注明原文地址:https://kaotiyun.com/show/ohWp777K
0

最新回复(0)