Lab - AP Computer Science

advertisement
ACSL
2006 - 2007
American Computer Science League
Contest #1
Intermediate Division
PROBABILITY
PROBLEM: Red marbles and Blue marbles are placed in a bag. One or more is
selected without looking in the bag. Find the probability of that event occurring. In this
program you will have 4 cases to examine:
Case 1: Just one marble is selected. As an example if 8 Red marbles and 2 Blue marbles
are placed in the bag, what is the probability of selecting a Red marble? The definition of
probability states the answer is found by counting the number of ways a Red marble can
8
occur and dividing by the total number of possible outcomes. This answer is
.
10
Case 2: Two marbles are selected with the first being replaced before the second is
drawn. As an example if 8 Red marbles and 2 Blue marbles are placed in the bag, what is
the probability of selecting a Red and a Blue in that order? By replacing the first marble,
the two events are independent. That is the selection of the first marble does not affect
the probability of the selecting the second marble. The answer is found by multiplying
8 2
16
* 
the probability of the 2 events:
.
10 10 100
Case 3: Two marbles are selected but this time the first marble is not replaced before the
second is drawn. As an example if 8 Red marbles and 2 Blue marbles are placed in the
bag, what is the probability of selecting a Red and a Blue in that order? By not replacing
the first marble, the two events are not independent. That is the selection of the first
marble does affect the probability of the selecting the second marble. The answer is
8 2 16
* 
found by multiplying the probability of the 2 events:
.
10 9 90
Case 4: Using the probability of NOT Red ( r ) or NOT Blue ( b ) in any of the above.
NOT Red is equal to 1 minus the probability of Red. As an example if 8 Red marbles
and 2 Blue marbles are placed in the bag, what is the probability of selecting, with
replacement, a NOT Red and a NOT Blue in that order? The answer is found by
8
2
16
multiplying the probability of the 2 events: ( 1  ) ( 1  ) 
10
10
100
INPUT: There will be 5 lines of inputs. The first character on each line will give the
number of marbles to be selected. The next 2 characters will give the number of each
colored marble in Red – Blue order. The next character will tell whether the event occurs
with (Y) or without replacement (N), and the final characters will give the event. Sample
input #1 states that 1 marble will be selected. There are 8 Red and 2 Blue marbles. The
event outcome is selecting a Red. Note since only 1 marble is being selected there is no
need to designate replacement. Sample Input #2 states 2 marbles will be selected. There
are 8 Red and 2 Blue marbles. There is no replacement of the first marble. The event is
selecting a Red and a Blue in that order.
OUTPUT: Print the probability of each event as a fraction. Do not reduce the answer.
SAMPLE INPUT
1. 1, 8, 2, R
2. 2, 8, 2, N, R, B
3. 2, 8, 2, Y, R, B
4. 2, 8, 2, Y, r, b
5. 2, 8, 2, Y, R, r
SAMPLE OUTPUT
1. 8/10
2. 16/90
3. 16/100
4. 16/100
5. 16/100
Download