#37877: 20%但不知道哪裡有錯誤,可以請各位幫忙看一下嗎


Betty314350 (Betty)


#include <iostream>
#include <math.h>
using namespace std;
 
int main() {
int a, b, c;
int d=sqrt(b*b-4*a*c);
int x1=(-1*b+d)/( 2*a);
int x2=(-1*b-d)/( 2*a);
 
cin>>a>> b>> c;
if (d==0)
cout<<"Two same roots x= "<<x1<<endl;
 
else if(d>0)
cout<<" Two different roots x1= "<<x1 <<" x2="<<x2<<endl;
 
else
cout<<"No real root"<<endl;
return 0;
}
 
#37879: Re: 20%但不知道哪裡有錯誤,可以請各位幫忙看一下嗎


predator (sigma)


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

cin>>a>> b>> c; 放錯地方了吧。

#37880: Re: 20%但不知道哪裡有錯誤,可以請各位幫忙看一下嗎


predator (sigma)


我是用 visual studio 練習 cpp

你這寫法,會提示錯誤。

#37882: Re: 20%但不知道哪裡有錯誤,可以請各位幫忙看一下嗎


Betty314350 (Betty)


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

cin>>a>> b>> c; 放錯地方了吧。

太謝謝你了,我後來把它放在int a, b, c;下面,就有通過了,但還是不太了解這兩個位置的差別在哪

#37883: Re: 20%但不知道哪裡有錯誤,可以請各位幫忙看一下嗎


Betty314350 (Betty)


我是用 visual studio 練習 cpp

你這寫法,會提示錯誤。


是語法錯誤嗎,但我用dev c++測試,是正確的

#37989: Re: 20%但不知道哪裡有錯誤,可以請各位幫忙看一下嗎


cges30901 (cges30901)


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

cin>>a>> b>> c; 放錯地方了吧。

太謝謝你了,我後來把它放在int a, b, c;下面,就有通過了,但還是不太了解這兩個位置的差別在哪


你還沒用cin輸入a,b,c的值,就拿來計算d,x1,x2,當然會出錯啊,你可以試試,cin之前a,b,c是多少就知道了