3个回答
展开全部
一个 n阶方阵,采用n×n(n行n列)的二维数组来存储:
其主对角线上的元素的下标,满足:行号 = 列号
其副对角线上的元素的下标,满足:行号 + 列号 = n + 1
VB代码实现:(以n = 5为例,并且数组为d,并已经赋值)
dim n as integer
dim S1 As Integer '主对角线上元素之和
dim S2 As Integer '副对角线上元素之和
dim i as integer
dim j as integer
n = 5
S1 = 0
S2 = 0
for i = 1 To 5
for j = 1 to n
if i = j Then s1 = s1 + d(i,j)
if i + j = n + 1 Then s2 = s2 + d(i,j)
next j
next i
Print "主对角线元素之和 = ";S1
Print "副对角线元素之和 = ";S2
其主对角线上的元素的下标,满足:行号 = 列号
其副对角线上的元素的下标,满足:行号 + 列号 = n + 1
VB代码实现:(以n = 5为例,并且数组为d,并已经赋值)
dim n as integer
dim S1 As Integer '主对角线上元素之和
dim S2 As Integer '副对角线上元素之和
dim i as integer
dim j as integer
n = 5
S1 = 0
S2 = 0
for i = 1 To 5
for j = 1 to n
if i = j Then s1 = s1 + d(i,j)
if i + j = n + 1 Then s2 = s2 + d(i,j)
next j
next i
Print "主对角线元素之和 = ";S1
Print "副对角线元素之和 = ";S2
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询