楼主 northwolves |
Q:分子分母不可约的真分数称为最简真分数,如分母不大于8的最简真分数:1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8。分母不大于3000并且值在1/3到1/2之间的最简真分数有多少个? A:循环。- Function GCD(a, b) As Long
- If b = 0 Then GCD = a: Exit Function
- GCD = GCD(b, a Mod b)
- End Function
- Sub main1()
- Dim i&, j&, num&
- For i = 5 To 3000
- For j = i \ 3 + 1 To i \ 2
- If GCD(i, j) = 1 Then num = num + 1
- Next j, i
- MsgBox num
- End Sub
Q: 能不能使用递归? A:有点不好理解:- Function count(ParamArray d()) As Long
- If d(2) + d(4) > d(0) Then Exit Function
- count = count(d(0), d(1) + d(3), d(2) + d(4), d(3), d(4)) + count(d(0), d(1), d(2), d(1) + d(3), d(2) + d(4)) + 1
- End Function
- Sub main2()
- MsgBox count(3000, 1, 3, 1, 2)
- End Sub
返回:456030 |