常常遇到輸出短少的情況,是哪個地方出了問題呢??
請高手指教,謝謝!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int a,b,c,d;
int a1,a2;
while(scanf ("%d%d%d",&a,&b,&c)!=EOF)
{
d=b*b-4*a*c;
if(d>0){
a1=(-b+sqrt(b*b-4*a*c))/(2*a);
a2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("Two different roots x1=%d , x2=%d",a1,a2);
}
else
if(d=0){
a2=-sqrt(c);
printf("Two same roots x=%d",a2);
}
else
if(d<0){
printf("No real root");
}
}
return 0;
}
else裡面
if(d == 0)
感謝您,我已作修正,並加了break,若不加break輸出的值將一直重複,但我加了break變輸出短少,請問該用哪個做修正呢?
不用break
你輸出沒有換行
printf("Two different roots x1=%d , x2=%d\n",a1,a2);
printf("Two same roots x=%d\n",a2);
printf("No real root\n");
然後重根那裏
應該是a2 = -b/(2*a)