Problem8:計算平均值(8%)
給你一組數字,請寫一個程式計算出這組數字的平均值,四捨五入至小數第2 位。
輸入說明:
第一行的數字,代表有幾個問題要求解。第二行開始的每一行,為一個獨立的問題。每
一行的第一個整數為這個問題所屬數字的數目n,其範圍為[2, 100]間的整數。接下來的n 個
整數,分別代表這n 個數字的數值,數值範圍為[1, 100]間的整數。
輸出說明:
對輸入的每個問題,分別以一行輸出平均數,輸出的格式請四捨五入至小數第二位。
輸入範例:
2
5 2 4 6 8 10
3 52 30 61
輸出範例:
6.00
47.67
Dim s
回覆刪除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, t
For i = 1 To t
Line Input #1, n
Call A1(n)
Next
Close
Close
End
End Sub
Sub A1(a)
Dim sum
s = Split(a)
For i = 1 To UBound(s)
sum = sum + Val(s(i))
Next
Dim kk As Double
kk = sum / s(0)
Print #2, Format(kk, "0.00")
End Sub
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 ii = 1 To N
Line Input #1, X
Y = Split(X)
total = 0
For i = 1 To UBound(Y)
total = total + Val(Y(i))
Next i
average = total / (UBound(Y))
Print #2, Format(average, "0.00")
Next ii
Close #2
Close #1
End
End Sub