求一个VB算法。 100
求:若干个数之和,最接近500的是哪几个?
算法怎么写啊? 展开
代码:
Private Sub Command1_Click()
Cls
Randomize
Dim Sum(5 To 20) As Integer, AbsoluteDeviation(5 To 20) As Integer, OriginalIndex(5 To 20) As Integer
Dim Rank(1 To 16) As Integer
For i = LBound(Sum) To UBound(Sum)
For j = 1 To i
Sum(i) = Sum(i) + Int(Rnd * 100 + 1)
AbsoluteDeviation(i) = Abs(Sum(i) - 500)
Next j
OriginalIndex(i) = i
Print "Sum(" & Format(i, "00") & ")=" & Format(Sum(i), "@@@@"), Format(AbsoluteDeviation(i), "@@@@")
Next i
AbsoluteDeviationSort Sum, AbsoluteDeviation, OriginalIndex, Rank
For i = LBound(AbsoluteDeviation) To UBound(AbsoluteDeviation)
Print "Sum(" & Format(OriginalIndex(i), "00") & ")=" & Format(Sum(i), "@@@@"), Format(AbsoluteDeviation(i), "@@@@") & Space(3) & Format(Rank(i - 4), "@@")
Next i
End Sub
Public Sub AbsoluteDeviationSort(s() As Integer, a() As Integer, o() As Integer, r() As Integer)
Dim b As Integer
For i = LBound(a) To UBound(a) - 1
For j = i + 1 To UBound(a)
If a(j) < a(i) Then
t = a(i)
a(i) = a(j)
a(j) = t
t = s(i)
s(i) = s(j)
s(j) = t
t = o(i)
o(i) = o(j)
o(j) = t
End If
Next j
Next i
r(1) = 1
For i = LBound(r) + 1 To UBound(r)
If a(i + 4) = a(i + 3) Then
r(i) = r(i - 1)
Else
r(i) = i
End If
Next i
End Sub
运行界面:
你答案偏题了,不对。
广告 您可能关注的内容 |