#10754: 最後一的測資過不了用float可是找不到bug


40375025H (eric)


#include <stdio.h>
#include <math.h>
int main(){
float a, b, c, D, S, x, x1, x2;

while(~scanf("%f %f %f", &a, &b ,&c)){

D = b*b - 4*a*c;

if(D > 0){
x1 = sqrt(D)/2*a - b/2*a;
x2 = -(b + sqrt(D))/2*a;
printf("Two different roots x1=%.0f , x2=%.0f\n", x1, x2);
}
else if(D == 0){

x =-b/2*a;
if(x == -0){
printf("Two same roots x=0\n");
}
else{

printf("Two same roots x=%.0f\n", x);
}
}
else{
printf("No real root\n");
}
}
return 0;
}

#10757: Re:最後一的測資過不了用float可是找不到bug


anandrewboy70900 (ShowTsai)


#include
#include
int main(){
float a, b, c, D, S, x, x1, x2;

while(~scanf("%f %f %f", &a, &b ,&c)){

D = b*b - 4*a*c;

if(D > 0){
x1 = sqrt(D)/(2*a) - b/(2*a);
x2 = -(b + sqrt(D))/(2*a);
printf("Two different roots x1=%.0f , x2=%.0f\n", x1, x2);
}
else if(D == 0){

x =-b/(2*a);
if(x == -0){
printf("Two same roots x=0\n");
}
else{

printf("Two same roots x=%.0f\n", x);
}
}
else{
printf("No real root\n");
}
}
return 0;
}