#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)==3){
if(a>0){
if((b*b-4*a*c)>0)
printf("Two different roots x1=%d , x2=%d\n",(-b+sqrt(b*b-4*a*c))/(2*a),(-b-sqrt(b*b-4*a*c))/(2*a));
if((b*b-4*a*c)==0)
printf("Two same roots x=%d\n",(-b)/(2*a));
if((b*b-4*a*c)<0)
printf("No real root\n");
}
if(a<0){
if((b*b-4*a*c)>0)
printf("Two different roots x1=%d , x2=%d\n",(-b-sqrt(b*b-4*a*c))/(2*a),(-b+sqrt(b*b-4*a*c))/(2*a));
if((b*b-4*a*c)==0)
printf("Two same roots x=%d\n",(-b)/(2*a));
if((b*b-4*a*c)<0)
printf("No real root\n");
}
}
return 0;
}
抱歉應該是這個,就是如果輸入1 4 3會出現x1=0,x2=-1074.....
不知道是為什麼= =
只能請更強的高手來解惑~~
目前我只知道是printf或是sqrt的問題....它會變的怪怪的
解決方法就是強制轉型....(其實他的名子我不知道是啥,看看就好)
printf("Two different roots x1=%d , x2=%d\n",(int)(-b+sqrt(b*b-4*a*c))/(2*a),(int)(-b-sqrt(b*b-4*a*c))/(2*a));