#24675: 求問哪裡錯? (C++)


EXOL (Judy)


//a006 一元二次方程式

#include<iostream>

#include<cmath>

using namespace std;

 

int main(){

int a, b, c;

cin >> a >> b >> c;

int D =sqrt(b*b-(4*a*c));

 

if(D>0){

cout << "Two different roots x1=" << (D-b)/(2*a) << " , x2=" << -(D+b)/(2*a) << endl;

}

else if(D<0) {

cout << "No real root" << endl;

}

else{

cout << "Two same roots " << "x=" << -(b/2*a) << endl;

}

 

return 0;

}

#25179: Re:求問哪裡錯? (C++)


000h (000h)


cout << "Two same roots " << "x=" << -(b/2*a) << endl;

乘除順序錯誤,應為:-b/(2*a)

*一開始我也沒注意到弄很多次XD