#26522: C++ NA:80% 這一題在最後都會卡關,求救 【您的答案為: Two same roots x=-4 正確答案為: Two same roots x=-1】


10835229@mail.hpsh.tp.edu.tw (我吃不下了,好飽)


#include <bits/stdc++.h>
using namespace std;

int main(){
int a,b,c;
int D,x1,x2;
cin>>a>>b>>c;
D=(b*b)-(4*a*c);
x1=((0-b)+sqrt(D))/2*a;
x2=((0-b)-sqrt(D))/2*a;
if(D>0)
cout<<"Two different roots x1="<<x1<<" , "<<"x2="<<x2<<endl;
else if(D==0&&x1==x2)
cout<<"Two same roots x="<<x2<<endl;
else
cout<<"No real root"<<endl;
return 0;
}
 
 
#26524: Re:C++ NA:80% 這一題在最後都會卡關,求救 【您的答案為: Two same roots x=-4 正確答案為: Two same roots x=-1】


a810183@gm.tnfsh.tn.edu.tw (丁時揚)


#include <bits/stdc++.h>
using namespace std;

int main(){
int a,b,c;
int D,x1,x2;
cin>>a>>b>>c;
D=(b*b)-(4*a*c);
x1=((0-b)+sqrt(D))/2*a;
x2=((0-b)-sqrt(D))/2*a;
if(D>0)
cout<<"Two different roots x1="<<x1<<" , "<<"x2="<<x2<<endl;
else if(D==0&&x1==x2)
cout<<"Two same roots x="<<x2<<endl;
else
cout<<"No real root"<<endl;
return 0;
}
 
 

重根的輸出獨立出來變成(-1*b)/2*a 可以解決這個問題



#26538: Re:C++ NA:80% 這一題在最後都會卡關,求救 【您的答案為: Two same roots x=-4 正確答案為: Two same roots x=-1】


cges30901 (cges30901)


#include <bits/stdc++.h>
using namespace std;

int main(){
int a,b,c;
int D,x1,x2;
cin>>a>>b>>c;
D=(b*b)-(4*a*c);
x1=((0-b)+sqrt(D))/2*a;
x2=((0-b)-sqrt(D))/2*a;
if(D>0)
cout<<"Two different roots x1="<<x1<<" , "<<"x2="<<x2<<endl;
else if(D==0&&x1==x2)
cout<<"Two same roots x="<<x2<<endl;
else
cout<<"No real root"<<endl;
return 0;
}
 
 

2*a要用括號包起來