比赛规则:去掉一个最高分和一个最低分,求出剩下得分的平均值
比赛规则:去掉一个最高分和一个最低分,求出剩下得分的平均值
排序啊,找到一组数的最大值和最小值然后把剩下的平均啊
Private Sub Command1_Click()
Dim a(1 To 5) As Integer
For i = 1 To 5
a(i) = InputBox("N")
Next i
Dim max, min, avsum As Single
max = a(1)
min = a(1)
For i = 1 To 5
If max <= a(i) Then
max = a(i)
End If
If min >= a(i) Then
min = a(i)
End If
Next i
For i = 1 To 5
Sum = Sum + a(i)
Next i
avsum = (Sum - max - min) / 3
Print a(1); a(2); a(3); a(4); a(5)
Print max; min; Sum; avsum
End Sub
楼上算法不错,我稍微拓展了一下:
Private Sub Command1_Click()
Dim a() As Integer,n as integer,i as integer,sum as long
n=val(inputbox("请输入评委的个数"))
Redim a(1 to n)
For i = 1 To n
a(i) = InputBox("请输入第" & i & "个分数")
Next i
Dim max, min, avsum As Single
max = a(1)
min = a(1)
For i = 1 To n
If max <= a(i) Then
max = a(i)
End If
If min >= a(i) Then
min = a(i)
End If
Next i
For i = 1 To n
sum = sum + a(i)
Next i
avsum = (sum - max - min) / (n-2)
for i=1 to n
print a(i)
next i
Print "Max=" & max;"Min=" & min;"Sun=" & sum; "Avsum=" & avsum
End Sub
这个要求不需要多浪费次循环排序。。。
Private Sub Command1_Click()
Dim a() As Integer, n As Integer, i As Integer
n = Val(InputBox("请输入评委的个数"))
ReDim a(1 To n)
For i = 1 To n
a(i) = InputBox("请输入第" & i & "个分数")
Next i
Dim NumMax!, NumMin!, NumSum!, AveSum As Single
NumMin = a(1): NumMax = a(1): NumSum = a(1)
For i = 2 To n
If a(i) > NumMax Then NumMax = a(i)
If a(i) < NumMin Then NumMin = a(i)
NumSum = NumSum + a(i)
Next i
AveSum = (NumSum - NumMax - NumMin) / (n - 2)
For i = 1 To n
Print a(i)
Next i
Print "Max=" & NumMax; " Min=" & NumMin; " Sum=" & NumSum; " AveSum=" & AveSum
End Sub
[此贴子已经被作者于2007-6-24 18:35:34编辑过]