#include<stdio.h>
#include<math.h>
int main(void)
{
int a,b,c,x,x1,x2;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
if(b * b - 4 * a * c > 0){
x1 = (-b+sqrt(b * b - 4 * a * c))/2*a;
x2 = (-b-sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
else if(b * b - 4 * a * c == 0){
x = - b / 2 * a;
printf("Two same roots x=%d\n",x);
}
else{
printf("No real root\n");
}
}
return 0;
}
#include
#include
int main(void)
{
int a,b,c,x,x1,x2;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){
if(b * b - 4 * a * c > 0){
x1 = (-b+sqrt(b * b - 4 * a * c))/2*a;
x2 = (-b-sqrt(b*b-4*a*c))/2*a;
printf("Two different roots x1=%d , x2=%d\n",x1,x2);
}
else if(b * b - 4 * a * c == 0){
x = - b / 2 * a; ///////////////////////////////////////注意
printf("Two same roots x=%d\n",x);
}
else{
printf("No real root\n");
}
}
return 0;
}
建議你,學習任何語言時,不確定其運算方式時,接用()來限定它
x = - b / 2 * a;
執行順序是
x = -b / 2;
x *= a;
ex: a = 3, b = 6 結果: x = - 9 但你希望是 -1
所以,應該寫成 x = -b / (2*a); 才對