猴子吃桃问题 递归VB

2024-12-26 17:19:23
推荐回答(5个)
回答1:

Private Sub Command1_Click()
Dim a, b, s As Integer
a = 1'第9天吃完后就是这一个了,令a=1
For i = 9 To 1 Step -1 '吃之前的桃子数 ,从第九天吃之前开始算起
a = (a + 1) * 2 '每天吃之前剩的桃子数
Next
Print a
End Sub
结果1534个,不是知道是什么猴子这么能吃。。。。

回答2:

Dim t As Integer

Function Tao(i As Integer) As Integer
If i < 10 Then '如果i<10则条件成立
t = (t + 1) * 2 计算前一天桃子的个数
i = i + 1 '再推前一天
Tao (i) '递归
End If
End Function

Private Sub Command1_Click()
t = 1
Tao (1) '调用Tao
Print t
'结果t=1534
End Sub

回答3:

Dim
t
As
Integer
Function
Tao(i
As
Integer)
As
Integer
If
i
<
10
Then
'如果i<10则条件成立
t
=
(t
+
1)
*
2
计算前一天桃子的个数
i
=
i
+
1
'再推前一天
Tao
(i)
'递归
End
If
End
Function
Private
Sub
Command1_Click()
t
=
1
Tao
(1)
'调用Tao
Print
t
'结果t=1534
End
Sub

回答4:

option explicit

dim i as integer,t as integer

t=1
for i = 8 to 1 step -1
t = 2 * ( t + 1 )
next
print t

回答5:

http://zhidao.baidu.com/question/76991927.html
这问题我已经回答过了~~你看看~