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


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

學校 : 不指定學校
編號 : 102829
來源 : [118.150.133.251]
最後登入時間 :
2021-08-11 15:43:37
a006. 一元二次方程式 | From: [118.150.133.251] | 發表日期 : 2021-08-11 16:40

#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 (丁時揚)

學校 : 不指定學校
編號 : 147663
來源 : [27.242.222.62]
最後登入時間 :
2021-11-04 23:51:59
a006. 一元二次方程式 | From: [110.26.158.52] | 發表日期 : 2021-08-11 17:28

#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)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
a006. 一元二次方程式 | From: [39.9.134.68] | 發表日期 : 2021-08-12 16:47

#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要用括號包起來

 
ZeroJudge Forum