--- title: 18-4:一元二次方程式 lang: zh-tw tags: DICE Python --- 18-4:一元二次方程式 === > [name=Chinglin-K] --- 目錄:[Dice 程式教學-Python完整版](https://hackmd.io/@Chinglin-K/Dice-menu) 上一篇:[18-3:算平方根](https://hackmd.io/@Chinglin-K/Dice-18-3) 下一篇:[18-5:算利息交給電腦](https://hackmd.io/@Chinglin-K/Dice-18-5) --- ## 題目 一元二次方程式 寫一個程式從標準輸入重覆取得 3 個整數: a b c, 直到 3 個整數皆為 0 止,輸出 "Game Over!!" 字樣。 請輸出 ax2+bx+c=0 的實數解, 若為兩相同實數,則僅需輸出一解,否則輸出兩解; 若無實數解,請輸出 "無實數解!!" 字樣 輸入範例: 4 4 1 1 0 -9 1 1 2 0 0 0 輸出範例: -0.500000 3.000000 -3.000000 無實數解!! Game Over!! --- ## 程式碼 ```Python= import math while True: a=int(input()) b=int(input()) c=int(input()) if(a==0 and b==0 and c==0): print("Game Over!!") break d=((b*b)-(4*a*c)) if(d>=0): d=math.sqrt(d) if(d>0): print(f'{(-b+d)/(2*a):.6f}',end=" ") print(f'{(-b-d)/(2*a):.6f}') elif(d==0): print(f'{(-b)/(2*a):.6f}') else: print("無實數解!!") ``` --- ## 輸出 ```Python= ``` --- 目錄:[Dice 程式教學-Python完整版](https://hackmd.io/@Chinglin-K/Dice-menu) 上一篇:[18-3:算平方根](https://hackmd.io/@Chinglin-K/Dice-18-3) 下一篇:[18-5:算利息交給電腦](https://hackmd.io/@Chinglin-K/Dice-18-5) --- :::info 「盡多少本分,得多少本事」😊 ::: --- {%hackmd i1nMRrZcTFmTvoF897K9zg %}