最後一題的答案為-1 , 但程式算出-4 , 有大神幫我找出程式的bug嗎感謝
include
#include
using namespace std;
int main()
{
int a;
int b;
int c;
cin >> a;
cin >> b;
cin >> c;
int D = pow(b, 2)-4*a*c;
if (D < 0)
{
printf("%s\n", "No real root");
}
else if(D == 0)
{
int x = -b / 2*a;
printf("%s", "Two same roots x=");
printf("%d", x);
}
else
{
int x1 = (-b + sqrt(D)) / 2*a;
int x2 = (-b - sqrt(D)) / 2*a;
printf("%s", "Two different roots x1=");
printf("%d", x1);
printf("%s", " , x2=");
printf("%d", x2);
}
}最後一題的答案為-1 , 但程式算出-4 , 有大神幫我找出程式的bug嗎感謝
2*a要括號