2012年11月20日 星期二

一元二次方程式


解一元二次方程式;輸入三個實數a、b、c,試解ax^2 + bx + c = 0 。
規則敘述:輸入三個實數;輸出 X 的值,若解不為虛根,則分別印出解,解和解之間以空格作為間隔。若為重根則只需要印出一個解即可,若為虛根則印出 “I can’t solve it !”。
輸出入說明
輸入:
–2,1,1
輸出:
-0.5
1

2 則留言:

  1. Dim a, b, c As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a, b, c
    a = Val(a): b = Val(b): c = Val(c)
    If ((b ^ 2) - (4 * a * c)) < 0 Then
    Print #2, "I can't solve it !"
    Else
    Print #2, ((-b) + ((b ^ 2) - (4 * a * c)) ^ 0.5) / (2 * a)
    Print #2, ((-b) - ((b ^ 2) - (4 * a * c)) ^ 0.5) / (2 * a)
    End If
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim a, b, c As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, a, b, c
    a = Val(a): b = Val(b): c = Val(c)
    If ((b ^ 2) - (4 * a * c)) < 0 Then
    Print #2, "I can’t solve it !"
    ElseIf ((b ^ 2) - (4 * a * c)) = 0 Then
    Print #2, ((-b) + ((b ^ 2) - (4 * a * c)) ^ 0.5) / (2 * a)
    Else
    Print #2, ((-b) + ((b ^ 2) - (4 * a * c)) ^ 0.5) / (2 * a)
    Print #2, ((-b) - ((b ^ 2) - (4 * a * c)) ^ 0.5) / (2 * a)
    End If
    Close #2
    Close #1
    End
    End Sub

    回覆刪除