a006.
一元二次方程式
| From: [140.126.196.44] |
發表日期
:
2009-07-09 11:52
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double a,b,c;
while(cin>>a>>b>>c){
double ansa,ansb;
if(a==0 && b==0 && c==0){
cout<<"No real root"<<endl;}
else if(a==0 && b!=0 && c!=0){
cout<<"Two same roots x="<<-c/b<<endl;}
else if(b*b-4*a*c==0){
ansa=-b/2*a;
ansb=-b/2*a;
if(ansa!=ansb){
continue;}
if(ansa==-0){
ansa==0;}
cout<<"Two same roots x="<<ansa<<endl;}
else if(b*b-4*a*c>0){
int temp;
temp=sqrt(b*b-4*a*c);
ansa=(-b+temp)/2*a;
ansb=(-b-temp)/2*a;
if(ansa<ansb){
swap(ansa,ansb);}
if(ansa==-0){
ansa==0;}
if(ansb==-0){
ansb==0;}
cout<<"Two different roots x1="<<ansa<<" , x2="<<ansb<<endl;}
else if(b*b-4*a*c<0){
cout<<"No real root"<<endl;}
}
return 0;
}