請教一下為什麼最後一個測資過不了
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a,b,c,x,y;
double D;
cin>>a>>b>>c;
x>=y;
D=b * b-4 * a * c;
if(D > 0)
{
x=0.5 * a *(-b + sqrt(b * b - 4* a * c));
y=0.5 * a *(-b - sqrt(b * b - 4* a * c));
cout<<"Two different roots x1="<< x <<" , x2="<<y;
}
else if(D ==0)
{
x=0.5 * a *(-b + sqrt(b * b - 4* a * c));
cout<<"Two same roots x="<<x;
}
else if(D<0){
cout<<"No real root";
}
}
請教一下為什麼最後一個測資過不了
#include
#include
using namespace std;
int main()
{
float a,b,c,x,y;
double D;
cin>>a>>b>>c;
x>=y;
D=b * b-4 * a * c;
if(D > 0)
{
x=0.5 * a *(-b + sqrt(b * b - 4* a * c));
y=0.5 * a *(-b - sqrt(b * b - 4* a * c));
cout<<"Two different roots x1="<< x <<" , x2="<<y;
}
else if(D ==0)
{
x=0.5 * a *(-b + sqrt(b * b - 4* a * c));
cout<<"Two same roots x="<<x;
}
else if(D<0){
cout<<"No real root";
}
}
首先,把資料型態int 改成float
再來x>=y這句不必要我刪了
最後,公式應該為x=(-b + sqrt(b * b - 4* a * c))/(2*a);
0.5 * a *(-b + sqrt(b * b - 4* a * c)); <一這個是( a *(-b + sqrt(b * b - 4* a * c)))/2
要除以a不是乘以a
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int a,b,c,x,y;
double D;
cin>>a>>b>>c;
D=b * b-4 * a * c;
if(D > 0)
{
x=(-b + sqrt(b * b - 4* a * c))/(2*a);
y=(-b - sqrt(b * b - 4* a * c))/(2*a);
cout<<"Two different roots x1="<< x <<" , x2="<<y<<endl;
}
else if(D ==0)
{
x=(-b - sqrt(b * b - 4* a * c))/(2*a);
cout<<"Two same roots x="<<x<<endl;
}
else if(D<0)
{
cout<<"No real root"<<endl;
}
}
請教一下為什麼最後一個測資過不了
#include
#include
using namespace std;
int main()
{
float a,b,c,x,y;
double D;
cin>>a>>b>>c;
x>=y;
D=b * b-4 * a * c;
if(D > 0)
{
x=0.5 * a *(-b + sqrt(b * b - 4* a * c));
y=0.5 * a *(-b - sqrt(b * b - 4* a * c));
cout<<"Two different roots x1="<< x <<" , x2="<<y;
}
else if(D ==0)
{
x=0.5 * a *(-b + sqrt(b * b - 4* a * c));
cout<<"Two same roots x="<<x;
}
else if(D<0){
cout<<"No real root";
}
}
首先,把資料型態int 改成float
再來x>=y這句不必要我刪了
最後,公式應該為x=(-b + sqrt(b * b - 4* a * c))/(2*a);
0.5 * a *(-b + sqrt(b * b - 4* a * c)); <一這個是( a *(-b + sqrt(b * b - 4* a * c)))/2
要除以a不是乘以a
#include
#include
using namespace std;
int main()
{
int a,b,c,x,y;
double D;
cin>>a>>b>>c;
D=b * b-4 * a * c;
if(D > 0)
{
x=(-b + sqrt(b * b - 4* a * c))/(2*a);
y=(-b - sqrt(b * b - 4* a * c))/(2*a);
cout<<"Two different roots x1="<< x <<" , x2="<<y<<endl;
}
else if(D ==0)
{
x=(-b - sqrt(b * b - 4* a * c))/(2*a);
cout<<"Two same roots x="<<x<<endl;
}
else if(D<0)
{
cout<<
非常感謝