# 解題紀錄 ## 題目:a006. 一元二次方程式 ## 📙 題目描述 求一元二次方程式 ax2+bx+c = 0 的根 ```txt 輸入說明 每組輸入共一行,內含三個整數 a, b, c 以空白隔開。 ``` ```txt 輸出說明 Two different roots x1=?? , x2=?? Two same roots x=?? No real root PS: 答案均為整數,若有兩個根則大者在前 ``` ```txt 範例輸入 #1 範例輸出 #1 1 3 -10 Two different roots x1=2 , x2=-5 ``` ```txt 範例輸入 #2 範例輸出 #2 1 0 0 Two same roots x=0 ``` ```txt 範例輸入 #3 範例輸出 #3 1 1 1 No real root ``` --- ## ✒️ 解題思路 這題需了解一元二次方程式判別式 當 $b^2 -4\cdot a\cdot c >=0$則有解, 等於0則重根 大於0則兩個不同的根 小於0則無實根存在 --- ## 💻 python解法 ```python a , b, c = map(int,input().split()) D = b**2 - 4*a*c if D < 0: print("No real root") elif D == 0: x = -b/2/a print("Two same roots x=%d" %x) else: x1,x2 = (-b + (D)**0.5)/2/a,(-b - (D)**0.5)/2/a print("Two different roots x1=%d , x2=%d"%(x1,x2)) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up