程式碼:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b,c,x1,x2;
cin>>a>>b>>c;
x1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);
x2=(-b-sqrt((b*b)-(4*a*c)))/(2*a);
if(x1!=x2)
{
cout<<"Two different roots x1="<<x1<<" , x2="<<x2;
}
else if(x1==x2)
{
cout<<"Two same roots x="<<x1;
}
else
{
cout<<"no real root";
}
}
#2說是要顯示No real root,但我的答案卻是Two same roots x=[]
程式碼:
#include
#include
using namespace std;
int main()
{
int a,b,c,x1,x2;
cin>>a>>b>>c;
x1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);
x2=(-b-sqrt((b*b)-(4*a*c)))/(2*a);
if(x1!=x2)
{
cout<<"Two different roots x1="<<x1<<" , x2="<<x2;
}
else if(x1==x2)
{
cout<<"Two same roots x="<<x1;
}
else
{
cout<<"no real root";
}
}
#2說是要顯示No real root,但我的答案卻是Two same roots x=[]
a,b,c應該用"double"宣告
另外虛擬碼應該是:
判斷a是否為0:
a!=0:
算判別式:d=pow(b,2.0f)-4.0f*a*c;(pow()應該在<cmath>標頭檔找到)
d是否大於0:
d>0:
解x:r[0]=(-b+pow(d,0.5f))/(2.0f*a);r[1]=(-b-pow(d,0.5f))/(2.0f*a);
輸出:"Two different roots x1=r[0] , x2=r[1]"
d=0:
解x:r[0]=(-b+pow(d,0.5f))/(2.0f*a)
輸出:"Two same roots x=r[0]"
d<0:
輸出:"No real root"
a=0:
a是否為0:
b!=0:
解x:r[0]=(-b+pow(d,0.5f))/(2.0f*a)
輸出:"Two same roots x=r[0]"
b=0:
輸出:"No real root"
程式碼:
#include
#include
using namespace std;
int main()
{
int a,b,c,x1,x2;
cin>>a>>b>>c;
x1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);
x2=(-b-sqrt((b*b)-(4*a*c)))/(2*a);
if(x1!=x2)
{
cout<<"Two different roots x1="<<x1<<" , x2="<<x2;
}
else if(x1==x2)
{
cout<<"Two same roots x="<<x1;
}
else
{
cout<<"no real root";
}
}
#2說是要顯示No real root,但我的答案卻是Two same roots x=[]
a,b,c應該用"double"宣告
另外虛擬碼應該是:
判斷a是否為0:
a!=0:
算判別式:d=pow(b,2.0f)-4.0f*a*c;(pow()應該在標頭檔找到)
d是否大於0:
d>0:
解x:r[0]=(-b+pow(d,0.5f))/(2.0f*a);r[1]=(-b-pow(d,0.5f))/(2.0f*a);
輸出:"Two different roots x1=r[0] , x2=r[1]"
d=0:
解x:r[0]=(-b+pow(d,0.5f))/(2.0f*a)
輸出:"Two same roots x=r[0]"
d<0:
輸出:"No real root"
a=0:
a是否為0:
b!=0:
解x:r[0]=(-b+pow(d,0.5f))/(2.0f*a)
輸出:"Two same roots x=r[0]"
b=0:
輸出:"No real root"
恩恩