下面不能正确计算 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + … + ( 1 + 2 + … + 10 ) 的是

admin2018-10-18  32

问题 下面不能正确计算 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + … + ( 1 + 2 + … + 10 ) 的是

选项 A、Private Sub Command1_Click()
s = 0 : t = 0
For i = 1 To 10
For j = 1 To i
t = t + j
Next j
s = s + t
Next i
Print s
End Sub
B、Private Sub Command1_Click()
s = 0
For i = 1 To 10
t = 0
For j = 1 To i
t = t + j
Next j
s = s + t
Next i
Print s
End Sub
C、Private Sub Command1_Click()
s = 0
For i = 10 To 1 Step -1
t = 0
For j = 1 To i
t = t + j
Next j
s = s + t
Next i
Print s
End Sub
D、Private Sub Command1_Click()
s = 0 : t = 0
For i = 1 To 10
t = t + i
s = s + t
Next i
Print s
End Sub

答案A

解析 选项A,当外层循环变量i=1时,执行内层循环,内层循环变量j=1时,执行结果为t=1,内层循环结束,s=1;当外层循环变量i=2时,执行内层循环,内层循环变量j=1时,t=1+1,内层循环变量j=2时,t=1+1+2,内层循环结束,s=1+t=1+(1+1+2),至此,A选项已经不符合要求。按照同样的方法分析B、C、D选项,均满足题目要求,故正确答案为选项A。
转载请注明原文地址:https://kaotiyun.com/show/3RFp777K
0

最新回复(0)