【 VB】4*4 矩阵对角线元素之和
PublicClassForm1PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventA...
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x(,) As Integer = {{1, 5, 0, 12}, {25, 3, 8, 10}, {6, 9, 11, 3}, {7, 15, 24, 5}}
Dim i, j As Integer
For i = 0 To 3
For j = 0 To i
Next
Next
Label1.Text = ""
For i = 0 To 3
Label1.Text &= vbCrLf
Label1.Text &= Space(i * 4)
For j = i To 3
Label1.Text &= Space(5 - Len(Str(x(i, j)))) & x(i, j)
Next
Next
For i = 0 To 3
For j = 0 To 3
x(i, j) = x(j, i)
Next
Next
Label2.Text = ""
For i = 0 To 3
Label2.Text &= vbCrLf
For j = 0 To 3
Label2.Text &= x(i, j) & Space(5 - Len(Str(x(i, j))))
Next
Next
Dim a, b As Integer
For i = 0 To 3
For j = 0 To 3
If i = j Then a = a + x(i, j)
If i + j = 3 Then b = b + x(i, j)
Next
Next
Label3.Text = "矩阵A的对角线元素和为"
Label3.Text &= a + b
End Sub
End Class
矩阵A的对角线元素和运行答案为52,而正确答案应为56,哪里错了呢。。。。。。。。。求解,另外,将上面上三角改为下三角怎么改。。。。。。。。。。 展开
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x(,) As Integer = {{1, 5, 0, 12}, {25, 3, 8, 10}, {6, 9, 11, 3}, {7, 15, 24, 5}}
Dim i, j As Integer
For i = 0 To 3
For j = 0 To i
Next
Next
Label1.Text = ""
For i = 0 To 3
Label1.Text &= vbCrLf
Label1.Text &= Space(i * 4)
For j = i To 3
Label1.Text &= Space(5 - Len(Str(x(i, j)))) & x(i, j)
Next
Next
For i = 0 To 3
For j = 0 To 3
x(i, j) = x(j, i)
Next
Next
Label2.Text = ""
For i = 0 To 3
Label2.Text &= vbCrLf
For j = 0 To 3
Label2.Text &= x(i, j) & Space(5 - Len(Str(x(i, j))))
Next
Next
Dim a, b As Integer
For i = 0 To 3
For j = 0 To 3
If i = j Then a = a + x(i, j)
If i + j = 3 Then b = b + x(i, j)
Next
Next
Label3.Text = "矩阵A的对角线元素和为"
Label3.Text &= a + b
End Sub
End Class
矩阵A的对角线元素和运行答案为52,而正确答案应为56,哪里错了呢。。。。。。。。。求解,另外,将上面上三角改为下三角怎么改。。。。。。。。。。 展开
展开全部
Dim a, b As Integer
For i = 0 To 3
For j = 0 To 3
If i = j Then a = a + x(i, j)
If i + j = 3 Then b = b + x(i, j)
Next
Next
改为
dim a,b as integer
for i=0 to 3
a+=x(i,i)
b+=x(i,3-i)
next
就行了
追问
结果还是52没变。。。。
追答
错误出于中间你对行列进行了置换,你现在计算的是转换后的数组的对角线。
For i = 0 To 3
For j = 0 To 3
x(i, j) = x(j, i)
Next
Next
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询