#14229: 請問這段程式碼是錯在哪呢 一直想不明白


andy5202andy5202@gmail.com (子恆黃)


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

#14230: Re:請問這段程式碼是錯在哪呢 一直想不明白


a0970580085@gmail.com (/)


#include
#include
#include
using namespace std;
int main () {
int a , b , c ;
while ( cin >> a >> b >> c ) {
int D = (b*b)-(4*a*c) ; 
int d = sqrt (D) ;
if ( D if ( D==0 ) cout << "Two same roots x=" << -b/2*a << endl ;
if ( D>0 ) cout << "Two different roots x1=" << (-b+d)/2*a << " , x2=" << (-b-d)/2*a << endl ;
}
return 0 ;
}



重根有誤  -b 除以 2a

你那樣寫變 -b 除以 2 乘以 a