麻烦谁能帮我解释下下面的计算过程,Private Sub command1_Click()Print p1(3,7)End SubPublic Function p1(x As Single,n As Integer) As SingleIf n = 0 Thenp1 = 1ElseIf n Mod 2 = 1 Thenp1 = x * p1(x,n \ 2)Elsep1 = p1(x,n \ 2) \ xEnd IfEnd IfE

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/18 18:01:01
麻烦谁能帮我解释下下面的计算过程,PrivateSubcommand1_Click()Printp1(3,7)EndSubPublicFunctionp1(xAsSingle,nAsInteger)A

麻烦谁能帮我解释下下面的计算过程,Private Sub command1_Click()Print p1(3,7)End SubPublic Function p1(x As Single,n As Integer) As SingleIf n = 0 Thenp1 = 1ElseIf n Mod 2 = 1 Thenp1 = x * p1(x,n \ 2)Elsep1 = p1(x,n \ 2) \ xEnd IfEnd IfE
麻烦谁能帮我解释下下面的计算过程,
Private Sub command1_Click()
Print p1(3,7)
End Sub
Public Function p1(x As Single,n As Integer) As Single
If n = 0 Then
p1 = 1
Else
If n Mod 2 = 1 Then
p1 = x * p1(x,n \ 2)
Else
p1 = p1(x,n \ 2) \ x
End If
End If
End Function

麻烦谁能帮我解释下下面的计算过程,Private Sub command1_Click()Print p1(3,7)End SubPublic Function p1(x As Single,n As Integer) As SingleIf n = 0 Thenp1 = 1ElseIf n Mod 2 = 1 Thenp1 = x * p1(x,n \ 2)Elsep1 = p1(x,n \ 2) \ xEnd IfEnd IfE
开始 7 Mod 2 = 1 所以 p1 = 3 * ①p1(3,7\ 2)
①=p1(3,3)
3 Mod 2 = 1 所以 p1 = 3 * ②p1(3,3\ 2)
②=p1(3,1)
1 mod 2=1 所以 p1=3*③p1(3,1\2)
③=p1(3,0)
n=0 所以 p1=1,循环结束
返回③=1
返回②=3*1
返回①=3*3*1
最终值=3*3*3*1
圆圈里面的数字就是p1(.)这个函数