|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Problem 1207 - average number of different buses
Thelma drives shuttle buses. There are 60 of them in the yard numbered 1 to 60. Each day that she works she is randomly assigned a bus. After 40 days of working, on average how many different buses has she driven? For example: If she just happened to be assigned bus number 9 each of her 40 days then she will have only driven one bus, or if she just happened to be assigned bus number 9 or bus number 3 on each of her 40 days she will have driven only 2 different buses.
Round your answer to 2 decimal places.
Solution Format/Example: 15.37
问题1207:平均开过几辆不同的公车?
某公交线路有60个司机60台车编号1-60、每天随机驾驶其中1台车。
问40天内,平均每个司机开过几辆不同编号的公车?
答案=29.37
我写了两种解法,第2种计算精度高一点。
- Sub test1()
- Dim i&, j&, m&, n&, r&, s&
- m = 60: n = 40
-
- For i = 0 To 10 ^ 4 - 1
- ReDim a(m - 1)
- For j = 1 To n
- r = Int(Rnd * m)
- a(r) = a(r) + 1
- Next
-
- For j = 0 To m - 1
- If a(j) Then s = s + 1
- Next
- Next
- Debug.Print s / i
- Stop
- End Sub
- Sub test2()
- Dim i&, j&, k&, m&, n&, r&, s&
- m = 60: n = 80
- ReDim a(m - 1)
- For i = 0 To m - 1
- a(i) = i
- Next
-
- For i = 0 To 10 ^ 3 - 1
- ReDim b(m - 1, m - 1)
- Randomize
- For k = 1 To n
- For j = 0 To m - 1
- r = Int(Rnd * (m - j)) + j
- t = a(r): a(r) = a(j): a(j) = t: b(j, t) = b(j, t) + 1
- Next
- Next
-
- For j = 0 To m - 1
- For k = 0 To m - 1
- If b(j, k) Then s = s + 1
- Next
- Next
- Next
- Debug.Print s / m / i
- Stop
- End Sub
复制代码 |
|