a006.
一元二次方程式
| From: [140.117.147.41] |
發表日期
:
2014-07-22 16:55
此解法 考量到 a=0,b=0,c=0的可能
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a,b,c,out1,out2;
while(cin>>a>>b>>c)
{
if(c==0){out1=0;out2=((b==0)?0:((a==0)?0:-b/a));}
else {out1=((-b+sqrt(abs(b*b-4*a*c)))/2/a );out2=((-b-sqrt(abs(b*b-4*a*c)))/2/a );}
if(abs(out1)==0)out1=0; if(abs(out2)==0) out2=0;
if(b*b-4*a*c<0||(a==0&&b==0&&c!=0))cout<<"No real root"<<endl;
else if(a==0&&b==0&&c==0) cout<<"Infinitely solutions"<<endl;
else {if(out1==out2) cout<<"Two same roots x="<<out1<<endl;
else cout<<"Two different roots x1="<<((a>0)?out1:out2)<<" , x2="<<(a>0?out2:out1)<<endl;
}
}
}