题目

一个由正整数构成的等边直角三角形,从三角形的顶部到底部有很多条不同的路径。对于每条路径,把路径上的数字加起来可以得到一个和,以下程序求最大的和并记录最大和路径上的各个数字。(注意:路径上的每一步只能从一个数走到下一层,它的正下方或者右下方的数)程序根据输入的行数生成构造一个三角形所需的随机数存入数组a,运行输出“三角形、最大和以及依次访问到的数据”,例如输入5,运行效果如右图所示。算法采用自上而下逐层顺推的方式,层上每个数字选择与上层中可能路径上的和的最大值累加,从而得到到达该数字的所有路径中的最大和,过程如下图。 (1) 如图所示三角形 ,其所有路径中的最大和为 (2) 请将以下程序代码中划线处补充完整。 Private Sub Command1_Click()     Dim a(1 To 100) As Integer, y(1 To 100) As String     Dim i, j, k, n, c, res As Integer     n = Val(Text1.Text) '数组 y 记录路径上的数字,n 为行数     For i = 1 To n * (n + 1) / 2         a(i) = Int(Rnd *10)     Next i     List1.AddItem Str(a(1))     y(1) = Str(a(1)): s = "": k = 1     For i = 2 To n                 For j = 1 To i             x = k + j - 1             s = s + Str(a(x)) + " "             If j = 1 Then                 y(x) = y(x - i + 1) + Str(a(x))                 a(x) = a(x) + a(x - i + 1)             ElseIf j = i Then                 y(x) = y(x - i) + Str(a(x))                 a(x) = a(x) + a(x - i)             ElseIf Then                 y(x) = y(x - i + 1) + Str(a(x))                 a(x) = a(x) + a(x - i + 1)             Else                 y(x) = y(x - i) + Str(a(x))                 a(x) = a(x) + a(x - i)             End If         Next j         List1.AddItem s         s = ""     Next i     For c = k To k + n - 1         If a(c) > Max Then Max = a(c): res = c     Next c     List1.AddItem "最大和是:" + Str(Max)     List1.AddItem "访问到的数据依次是:" + End Sub 答案: 【1】19 【1】k = k + i – 1【2】a(x - i + 1) > a(x - i) 或a(x - i)< a(x - i + 1) a(x - i + 1) >= a(x - i) 或a(x - i)<= a(x - i + 1【3】y(res)
信息技术 试题推荐