迴圈

輸入a  b  c

判別 b^2 - 4ac 是否 >= 0

 

設立判別式delta = b^2 - 4ac

兩根 = (-b + delta) / 2a   ,    (-b - delta) / 2a

判別delta是否 = 0

輸出兩根

否(重根)

輸出其中一個

 

 

 

無實數解

 

*不能先設立delta*

*因為負數不能開根號*

 

 

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c, root1, root2, delta;

    while(cin >> a >> b >> c)
    {
        if((b*b - 4*a*c) >= 0)
        {
            delta = sqrt(b*b - 4*a*c);
            root1 = (-b + delta)/(2*a);
            root2 = (-b - delta)/(2*a);

            if(delta != 0)
                cout << "Two different roots x1=" << root1 << " , x2=" << root2 << endl;
            else
                cout << "Two same roots x=" << root1 << endl;
        }
        else
            cout << "No real root" << endl;
    }

    return 0;
}

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()