VB:有20个10以内的随机整数排成一排,找出每四个相邻数之和中的最大值,并指出是哪四个相邻的数
VB:有20个10以内的随机整数排成一排,找出每四个相邻数之和中的最大值,并指出是哪四个相邻的数
B(1)=A(1)+A(2)+A(3)+A(4)
B(20)=A(20)+A(1)+A(2)+A(3)
随机生成数组A,用循环结构求出每四个相邻数之和,用数组B保存
'把所有的与最大值相同的都显示出来
Private Sub Form_Load()
Me.AutoRedraw = True
Dim a(0 To 19) As Integer
Dim b(0 To 19) As Integer
Dim Max As Integer
Dim i As Integer
Max = 0
Print "随机生成的整数:";
For i = 0 To 19
Randomize
a(i) = Int(Rnd * 11)
Print a(i);
Next
Print
Print "被累加后的整数:";
For i = 0 To 19
b(i) = a(i Mod 20) + a((i + 1) Mod 20) + a((i + 2) Mod 20) + a((i + 3) Mod 20)
If b(Max) Print b(i);
Next
Print
FindData b, b(Max), a
'Print "最大值:" & b(Max)
'Print "相邻的数:" & a(Max Mod 20); a((Max + 1) Mod 20); a((Max + 2) Mod 20); a((Max + 3) Mod 20)
End Sub
Function FindData(arr() As Integer, ByVal MaxI As Integer, arra() As Integer)
Dim i As Integer
For i = 0 To UBound(arr)
If arr(i) = MaxI Then
Print "最大值:" & arr(i)
Print "相邻的数:" & arra(i Mod 20); arra((i + 1) Mod 20); arra((i + 2) Mod 20); arra((i + 3) Mod 20)
End If
Next
End Function