Description
求a*x^2+b*x+c=0方程的根。
Input
輸入有若干行,每行三個實數表示a、b、c。以0 0 0表示輸入結束。
Ouput
對于每一行,如果無解就輸出“Unanswered”。如果有解就按從小到大的方式輸出,格式為:“x1=1.00 x2=2.00”。(結果保留2位小數)
Sample Input
1 -2 1
1 1 1
1 0 -1.21
0 0 0
Sample Output
x1=1.00 x2=1.00
Unanswered
x1=-1.10 x2=1.10
#include<stdio.h>
#include<math.h>
int main()
{
double x1,x2;
double a,b,c,m,t;
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
{
m=b*b-4*a*c;
if(a==0&&b==0&&c==0)
{
return 0;
}
if(m<0)
{
x1=(-b-sqrt(b*b-4*a*c))/2*a;
x2=(-b+sqrt(b*b-4*a*c))/2*a;
printf("Unanswered\n");
continue;
}
if(m==0)
{
x1=(-b-sqrt(b*b-4*a*c))/2*a;
x2=(-b+sqrt(b*b-4*a*c))/2*a;
printf("x1=%.2lf x2=%.2lf\n",x1,x2);
}
if(m>0)
{
x1=(-b-sqrt(b*b-4*a*c))/2*a;
x2=(-b+sqrt(b*b-4*a*c))/2*a;
if(x1>x2)
{
t=x1;
x1=x2;
x2=t;
}
printf("x1=%.2lf x2=%.2lf\n",x1,x2);
}
}
return 0;
}
我就是因為輸入-2 5 0才不對的,請各位大佬幫我看看道理哪里不對了,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/203997.html
標籤:C語言
