#5096: WA 找不到哪裡錯 幫幫我><


s8603050 (jerry)


*** 第 5 點 (20%):WA (line:1)
您的答案為: Two same roots x=-4
正確答案為: Two same roots x=-1
以下是我的程式碼:

#include <iostream>
using namespace std;
#include <math.h>

int main()
{
 double a,b,c,x1,x2;
 while(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((b*b-4*a*c) > 0)
  {
   if((x1-x2) > 0)
    cout<<"Two different roots x1="<<x1<<" , "<<"x2="<<x2<<endl;
   else
    cout<<"Two different roots x1="<<x2<<" , "<<"x2="<<x1<<endl;
  }
  else
   if((b*b-4*a*c) == 0)
   {
    if(x1 == x2)
     cout<<"Two same roots x="<<x1<<endl;
   }
  else
   if((b*b-4*a*c) < 0)
    cout<<"No real root"<<endl;
 }
 return 0;
}

我2根那邊不知道出了什麼問題.....

#5097: Re:WA 找不到哪裡錯 幫幫我><


CSE911515 (蘇煒翔你好過分)


2*a要括號起來

還有如果b*b-4*a*c>0的話

x1就一定大於x2

因為開根號之後會是正數 

#5098: Re:WA 找不到哪裡錯 幫幫我><


s8603050 (jerry)


2*a要括號起來

還有如果b*b-4*a*c>0的話

x1就一定大於x2

因為開根號之後會是正數 

解決了,感恩^^