解题思路:A矩阵放a数组,B矩阵放b数组,相乘后的结果放在d数组
矩阵相乘的方式为:
⒈用A的第1行各个数与B的第1列各个数对应相乘相加,得到的结果就是第1行第1列的数;
⒉用A的第1行各个数与B的第2列各个数对应相乘相加,得到的结果是第1行第2列的数;
……
⒋依次进行,(直到)用A的最末行各个数与B的第末列各个数对应相乘相加,结果是最末行第末列的数。
图 a
图 b
Dim a(1 To 9) As Integer, b(1 To 9) As Integer '矩阵 A,B 的数组
Private Sub Command1_Click()
Dim s As String, i As Integer, ch As String
Dim x As Integer, y As Integer, k As Integer
Dim d(1 To 9) As Integer '新矩阵的数组
s = Text1.Text 'B 矩阵以字符串的形式输入
k = 1
'输入 B 矩阵的值
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch = "0" Or ch = "1" Then
b(k) = Val(ch)
k = k + 1
If k = 10 Then Exit For
End If
Next i
'计算出新的矩阵的值
For i = 1 To 9
For j = 1 To 3
Next j
Next i
'输出新矩阵的值
For i = 1 To 3
ch = ""
For j = 1 To 3
ch = ch + Str(d((i - 1) * 3 + j)) + " "
Next j
List2.AddItem ch
Next i
Max = 1
For i = 2 To 9
If d(Max) < d(i) Then Max = i
Next i
x = ((Max - 1) \ 3 + 1)
y =
Label2.Caption = "最大的数是" + Str(d(Max)) + "位置(" + Str(x) + "," + Str(y) +
")"
End Sub
Private Sub Form_Load()
Dim i As Integer, j As Integer, s As String
s = ""
Randomize
'生成矩阵 A 数组
For i = 1 To 9
a(i) = Int(Rnd * 20 + 1) * (-1) ^ (Int(Rnd * 2))
If Then
List1.AddItem s
s = ""
End If
s = s + " " + Str(a(i))
Next i
List1.AddItem s
End Sub