#7053: Na,逾时了,请高手赐教


xuzefeng (神)


#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main(void)
{ float a,b,c;
  float x1,x2;
 while(scanf("%d %d %d",&a,&b,&c))
{
  x1=(-b+sqrt(b*b-4*a*c))/2*a;
  x2=(-b-sqrt(b*b-4*a*c))/2*a;
  if((b*b-4*a*c)>0)
    printf("Two different roots x1=%.0f , x2=%.0f \n",x1,x2);
  if((b*b-4*a*c)==0) 
    printf("Two same roots x=%.0f \n",x1);
  if((b*b-4*a*c)<0) 
    printf("No real root \n");
 }
  return 0;
}
#7054: Re:Na,逾时了,请高手赐教


akira0331 (小迷糊)


#include
#include
#include
int main(void)
{ float a,b,c;
  float x1,x2;
 while(scanf("%d %d %d",&a,&b,&c))
{
  x1=(-b+sqrt(b*b-4*a*c))/2*a;
  x2=(-b-sqrt(b*b-4*a*c))/2*a;
  if((b*b-4*a*c)>0)
    printf("Two different roots x1=%.0f , x2=%.0f \n",x1,x2);
  if((b*b-4*a*c)==0) 
    printf("Two same roots x=%.0f \n",x1);
  if((b*b-4*a*c)<0) 
    printf("No real root \n");
 }
  return 0;
}


建議將X1,X2的運算放在 if((b*b-4*a*c)>0){ }裡面,不然遇到a=0會出現錯誤