#21052: 我不想用#include <math.h>根號,難道電腦看不懂^(1/2)


derek1403 (k)

學校 : 不指定學校
編號 : 119509
來源 : [58.114.214.125]
最後登入時間 :
2020-04-05 17:32:18
a006. 一元二次方程式 | From: [58.114.214.125] | 發表日期 : 2020-04-05 19:02

 

以下是錯的,有試過加#include <math.h>,但也不行。我的思路是觀察判別式D的情況,即:D>0 兩相異根、D=0重根、D<0無實根,能否再不要改變^(1/2)的情況下,寫出AC

include<iostream>
using namespace std;
int main()
{
int a, b, c, d, e, D;
while (cin >> a >> b >> c)
{D=b ^ 2 - 4 * a * c;
d = (-b + (b ^ 2 - 4 * a * c) ^ (1 / 2)) / (2 * a);
e = (-b - (b ^ 2 - 4 * a * c) ^ (1 / 2)) / (2 * a);
if (D == 0)){ cout << "Two same roots x=" << d }
if (D > 0){cout << "Two different roots x1=" << d << " , x2" = << e}
if (D < 0){cout << "No real root"}
}
}

 

有試過 b^2  改成(b^2)也是不行

 
#21053: Re:我不想用#include <math.h>根號,難道電腦看不懂^(1/2)


314159265358979323846264338327 ... (少年π)

學校 : 臺北市私立延平高級中學
編號 : 69058
來源 : [223.137.74.225]
最後登入時間 :
2024-04-18 19:26:56
a006. 一元二次方程式 | From: [111.71.29.28] | 發表日期 : 2020-04-05 19:37

 

 

以下是錯的,有試過加#include ,但也不行。我的思路是觀察判別式D的情況,即:D>0 兩相異根、D=0重根、D<0無實根,能否再不要改變^(1/2)的情況下,寫出AC

include
using namespace std;
int main()
{
int a, b, c, d, e, D;
while (cin >> a >> b >> c)
{D=b ^ 2 - 4 * a * c;
d = (-b + (b ^ 2 - 4 * a * c) ^ (1 / 2)) / (2 * a);
e = (-b - (b ^ 2 - 4 * a * c) ^ (1 / 2)) / (2 * a);
if (D == 0)){ cout << "Two same roots x=" << d }
if (D > 0){cout << "Two different roots x1=" << d << " , x2" = << e}
if (D < 0){cout << "No real root"}
}
}

 

有試過 b^2  改成(b^2)也是不行

C++的"^"是Xor運算而不是求冪的意思,所以不能這樣寫


 
ZeroJudge Forum