#第七章第二十四题(仿真:优惠券收集问题)(Simulation: coupon collection problem)
-
**7.24(仿真:优惠券收集问题)优惠券收集问题是一个经典的统计问题,他有很多实际应用。这个问题是重复地从一组对象中拿出一个对象,然后求出要讲所有对象都至少拿出来一次,需要拿多少次。该问题的一个变体是,从一副打乱的52张牌中重复选牌,直到每种花色都选过一次,需要选多少次。假设在选下一张牌之间,将选出来的牌放回去。编写程序,模拟要得到四张不同花色的牌所需要的的选取次数,然后显示选中的四张牌(有可能一张牌被选了两次)。下面是这个程序的运行示例:
King of Spade
2 of Heart
8 of Diamond
Jack of Club
Numbers of picks: 7
**7.24(Simulation: coupon collection problem)Coupon collection is a classic statistical problem, which has many practical applications. The problem is to repeatedly take an object out of a set of objects, and then figure out how many times it takes to get all the objects out at least once. A variant of this problem is to choose cards repeatedly from a disordered pack of 52 cards until each suit has been selected once, and how many times it needs to be selected. Suppose you put the card back between the next card. Write a program to simulate the number of times to get four cards of different colors, and then display the selected four cards (it is possible that a card has been selected twice). Here is an example of how to run this program:
King of Spade
2 of Heart
8 of Diamond
Jack of Club
Numbers of picks: 7 -
参考代码:
package chapter07; public class Code_24 { public static void main(String[] args) { String[] suit = { "Spade", "Heart", "Diamond", "Club"};