题目基本来自c经典编程,用vba全部重新写了,可以作为代码的参考 '【程序1】 '题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月 '后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总对数为多少? '1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21.... '2.程序代码 Sub prog1() ' Dim tuji, f(20) n = 20 For i = 1 To 20 If i = 1 Then f(i) = 1 If i = 2 Then f(i) = 1 If i > 2 Then f(i) = f(i - 1) + f(i - 2) End If Next MsgBox CStr(f(20)) End Sub ' 【程序2】 '题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? Sub prog2() ' Dim jilu(100), strjilu j = 1 For i = 1 To 100 jilu(i) = 0 Next i For i = 1 To 10000 If Sqr(i + 100) = Int(Sqr(i + 100)) Then If Sqr(i + 268) = Int(Sqr(i + 268)) Then jilu(j) = i j = j + 1 'MsgBox "该数为" & CStr(i) 'Exit For End If End If Next If jilu(1) = 0 Then MsgBox "meiyou" Else For i = 1 To 100 If jilu(i) <> 0 Then strjilu = strjilu + "||" + CStr(jilu(i)) End If Next MsgBox "该数为" & strjilu End If End Sub
|