如何使用vb语言 构造M*M的方阵对角线设置为1其他为0?

来源:百度知道 编辑:UC知道 时间:2024/07/02 04:06:32
急用哈~~~~谢谢各位啦~~~

Private Sub Command1_Click()
边长 = 10
ReDim AR(1 To 边长, 1 To 边长) As Integer
Dim x ,y As Integer
For x = 1 To 边长
For y = 1 To 边长
If (x = y) Or (x = 边长 - y + 1) Then
AR(x, y) = 1
Else
AR(x, y) = 0
End If
Print AR(x, y); '输出
Next y
Print ''输出换行
Next x
End Sub

dim a(1 to M,1 to M) as integer'也可以只用a(M,M)
dim i as integer
dim j as integer
for i=1 to M
for j=1 to M
if i=j then a(i,j)=1
else a(i,j)=0
endif
next j
next i

二楼的对,先看对角线的位置规律,左对角线的位置特点是行和列的坐标相同,即X=Y,右对解线的特点是行和列的坐标相加等于边长加1。