小弟寫的程式碼如下:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,c;
while(cin>>a>>b>>c)
if(b*b-4*a*c==0)
cout<<"Two same roots x="<<-b/2*a<<endl;
else if(b*b-4*a*c<0)
cout<<"No real root"<<endl;
else if(b*b-4*a*c>0)
cout<<"Two different roots x1="<<(-b+sqrt(b*b-4*a*c))/2*a <<" , x2="<< (-b-sqrt(b*b-4*a*c))/2*a<<endl;
}
前面4個全部ac
為什麼第5個wa??
請高手幫忙
小弟寫的程式碼如下:
#include
#include
using namespace std;
int main()
{
int a,b,c;
while(cin>>a>>b>>c)
if(b*b-4*a*c==0)
cout<<"Two same roots x="<<-b/2*a<<endl;
else if(b*b-4*a*c<0)
cout<<"No real root"<<endl;
else if(b*b-4*a*c>0)
cout<<"Two different roots x1="<<(-b+sqrt(b*b-4*a*c))/2*a <<" , x2="<< (-b-sqrt(b*b-4*a*c))/2*a<<endl;
}
前面4個全部ac
為什麼第5個wa??
請高手幫忙
先乘除後加減 由左至右
最後一行
cout<<"Two different roots x1="<<(-b+sqrt(b*b-4*a*c))/2*a <<" , x2="<< (-b-sqrt(b*b-4*a*c))/2*a<<endl;
我其實不知道是不是這個原因