2010年7月30日 星期五

高中 a006: 一元二次方程式



想看題目請點我

#include <iostream>
#include <math.h>
using namespace std;
  
int main() {  
    double a , b , c , x , x1 , x2 ;
    while(cin >> a >> b >> c)
    {
        if(((b * b) - (4 * a * c)) > 0)
        {
            x1 = ((( 0 - b ) - sqrt((b * b) - (4 * a * c))) / (2 * a)) ;
            x2 = ((( 0 - b ) + sqrt((b * b) - (4 * a * c))) / (2 * a)) ;
            if(x2 > x1)
            {
                int temp ;
                temp = x1 ;
                x1 = x2 ;
                x2 = temp ;
            }
            cout << "Two different roots x1=" << x1 << " , x2=" << x2 << endl;
        }
        else if (((b * b) - (4 * a * c)) == 0)
        {
            x = ((( 0 - b ) + sqrt((b * b) - (4 * a * c))) / (2 * a)) ;
            cout << "Two same roots x=" << x << endl ;
        }
        else
        {
            cout << "No real root" << endl ;
        }
    }
    return 0;  
}