以下是錯的,有試過加#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)也是不行
以下是錯的,有試過加#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運算而不是求冪的意思,所以不能這樣寫