2012年11月5日 星期一

計算平均值

給你一組數字,請寫一個程式計算出這組數字的平均值,四捨五入至小數第2 位。
輸入說明:
第一行的數字,代表有幾個問題要求解。第二行開始的每一行,為一個獨立的問題。每
一行的第一個整數為這個問題所屬數字的數目n,其範圍為[2, 100]間的整數。接下來的n 個
整數,分別代表這n 個數字的數值,數值範圍為[1, 100]間的整數。
輸出說明:
對輸入的每個問題,分別以一行輸出平均數,輸出的格式請四捨五入至小數第二位。
輸入範例:
2
5 2 4 6 8 10
3 52 30 61
輸出範例:
6.00
47.67

4 則留言:

  1. Dim times, num(999), temp(999) As Integer
    Dim ans(999) As Long
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, times

    For i = 1 To times
    Input #1, num(i - 1)
    For ii = 1 To num(i - 1)
    Input #1, temp(ii - 1)
    ans(i - 1) = ans(i - 1) + temp(ii - 1)
    Next
    Print #2, Format(ans(i - 1) / num(i - 1), "##0.00")
    Next

    Close #2
    Close #1

    End Sub

    回覆刪除
  2. 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, n
    For i = 1 To n
    z = 0
    Input #1, a
    For j = 1 To a
    Input #1, t
    z = z + t
    Next
    s = z / a
    Print #2, Format(s, "0.00")
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. 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, x
    For i = 1 To x

    Line Input #1, s
    s = Split(s)
    For j = 1 To UBound(s)
    a = a + Val(s(j))
    Next
    a = a / (j - 1)
    a = Format(a, "##0.00")
    Print #2, a
    a = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除