猴子吃桃子,每天刚好吃一半多一个,到第九天的时候,还剩下一个桃子,问原来一共有多少个桃子?
题目还有这样提示:假设第N天桃子为X,则前一天的桃子为M=(N+1)*2,以此类推,可知第八天的桃子为4,第七天的桃子为10...我想了好久,还是算法不晓得,总是做错下面是我的代码:Private Sub CmdExit_Click()
    Unload Me
End SubPrivate Sub CmdOK_Click()
    Dim i As Long               '天数
    Dim intInput As Integer     '输入的数据
    Dim intOutput As Double     '输出的数据
    intInput = CInt(TxtInput)
    
    If intInput <= 0 Then
        MsgBox "请输入整数...", vbInformation + vbOKOnly, "数据非法"
        GoTo LBLEND
    End If
    
就是这里的算法总是算错答案,我头都大了
LBLEND:
End Sub

解决方案 »

  1.   

    Private Sub Command1_Click()
        t = InputBox("天数:")
        s = 1
        For i = t - 1 To 1 Step -1
            s = (s + 1) * 2
        Next
        MsgBox s
    End Sub
      

  2.   

    Function sum(ttt)If ttt <> 9 Then
    ttt = ttt + 1
    sum = (sum(ttt) + 1) * 2
    Else
    sum = 1
    End If
    End Function典型的递归问题
      

  3.   

    Private Sub Command1_Click()
    On Error GoTo MyError
    Dim L As Long, i As Long
    L = 1
    For i = 2 To Val(Text1.Text)
        L = 2 * (L + 1)
    Next iMyError:
    MsgBox L
    End Sub
      

  4.   

    Dim i As Integer, sum As Integer
    sum = 1
    For i = 9 To 2 Step -1
        sum = 2 * (sum + 1)
    Next
    MsgBox sum
      

  5.   

    Private Sub Command1_Click()
       Dim t, s, i
        t = InputBox("天数:")
        s = 1
        For i = 8 To t Step -1
            s = (s + 1) * 2
        Next
        MsgBox s
    End Sub
      

  6.   

    谢谢大家,我基础还不扎实
    我才大一第一个学期
    放寒假看看下期开学要学的VB
    说实话
    我寒假才接触的VB