Zerojudge 基礎題庫a006 一元二次方程式

第一眼看到題目,感覺不是太難,腦中浮現了一個架構:

1. 輸入a,b,c
2.檢查答案數量 (b^2 -4ac)
->
if 數量=0
輸出

if數量=1
運算答案
輸出

if數量=2
運算答案
輸出
<-
//結束

所以就順手寫出這段程式碼
-----------------------------------------------------------------------------------

















-----------------------------------------------------------------------------------

但發現到當輸入 1 0 0 時
會跑出 Two same roots x=-0
0 前面有負號
這可不行

所以我在輸出區塊加了 當x==-0 ,x=0
-----------------------------------------------------------------------------------
(前面相同)

    if(D < 0)
    {
        cout << "No real root" << endl;
    }
    else if(D == 0)
    {
        cout << "Two same roots";

        tem = -b /2*a;
        if (tem == -0){
            tem = 0;
        }
        cout << " x=" << tem << endl;

        //cal answer

    }
    else if(D > 0)
    {
        cout << "Two different roots";
        tem = (-b + sqrt(b*b - 4*a*c))/2*a;
        if (tem == -0){
            tem = 0;
        }
        cout << " x1=" << tem << " ";


        cout << ",";
        tem = (-b - sqrt(b*b - 4*a*c))/2*a;
        if (tem == -0){
            tem = 0;
        }
        cout << " x2=" << tem << endl;


    }




}
-----------------------------------------------------------------------------------
當然,因為要不斷輸入進去,所以
cin >> a >> b >> c;  要變成 while (cin >> a >> b >> c){}
-----------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{


    //declaration
    float a,b,c,D,ans,tem;
    //input a,b,c
    while(cin >> a >> b >> c){
        //check answers quantity (D)
        D = b * b - 4 * a * c;


        if(D < 0)
        {
            cout << "No real root" << endl;
        }
        else if(D == 0)
        {
            cout << "Two same roots";

            tem = -b /2*a;
            if (tem == -0)
            {
                tem = 0;
            }
            cout << " x=" << tem << endl;

            //cal answer

        }
        else if(D > 0)
        {
            cout << "Two different roots";
            tem = (-b + sqrt(b*b - 4*a*c))/2*a;
            if (tem == -0)
            {
                tem = 0;
            }
            cout << " x1=" << tem << " ";


            cout << ",";
            tem = (-b - sqrt(b*b - 4*a*c))/2*a;
            if (tem == -0)
            {
                tem = 0;
            }
            cout << " x2=" << tem << endl;


        }



    }


}
-----------------------------------------------------------------------------------
這樣
1 3 -10
>>>Two different roots x1=2 , x2=-5
1 0 0
>>>Two same roots x=0
1 1 1
>>>No real root
範例正確!

送進zerojudge,結果...

您的答案為: Two same roots x=-4
正確答案為: Two same roots x=-1

為何會發生這種事?
我想了很久,最後在 tem = -b /2*a; 做一點手腳
加個括號,變成 tem = -b/(2*a)
然後...


通過檢測
為何沒加括號差這麼多?
我想是因為:
當沒加括號時,會先-b/2再*a
-b/2可能會跑出無限循環小數,算出的結果自然會有差(浮點樹只能存特定位數)

最後答案:
-----------------------------------------------------------------------------------






















留言

這個網誌中的熱門文章

Zerojudge 基礎題庫a004 文文的求婚 (Python)

紙蜻蜓的受風面積與紙蜻蜓落地時間的關係 #1 [實驗歷程與Python Matplotlib]

Zerojudge 基礎題庫a013 羅馬數字 (Python)