Java Reference
In-Depth Information
RLRRLRR. Display the final buildup of the balls in the slots in a histogram. Here
is a sample run of the program:
Enter the number of balls to drop: 5
Enter the number of slots in the bean machine: 8
LRLRLRR
RRLLLRR
LLRLLRR
RRLLLLL
LRLRRLR
O
O
OOO
( Hint : Create an array named slots . Each element in slots stores the num-
ber of balls in a slot. Each ball falls into a slot via a path. The number of Rs in
a path is the position of the slot where the ball falls. For example, for the path
LRLRLRR, the ball falls into slots[4] , and for the path is RRLLLLL, the ball
falls into slots[2] .)
***7.22
( Game: Eight Queens ) The classic Eight Queens puzzle is to place eight queens
on a chessboard such that no two queens can attack each other (i.e., no two queens
are on the same row, same column, or same diagonal). There are many possible
solutions. Write a program that displays one such solution. A sample output is
shown below:
|Q| | | | | | | |
| | | | |Q| | | |
| | | | | | | |Q|
| | | | | |Q| | |
| | |Q| | | | | |
| | | | | | |Q| |
| |Q| | | | | | |
| | | |Q| | | | |
**7.23
( Game: locker puzzle ) A school has 100 lockers and 100 students. All lockers are
closed on the first day of school. As the students enter, the first student, denoted
S1, opens every locker. Then the second student, S2, begins with the second
locker, denoted L2, and closes every other locker. Student S3 begins with the
third locker and changes every third locker (closes it if it was open, and opens it if
it was closed). Student S4 begins with locker L4 and changes every fourth locker.
Student S5 starts with L5 and changes every fifth locker, and so on, until student
S100 changes L100.
After all the students have passed through the building and changed the lockers,
which lockers are open? Write a program to find your answer and display all
open locker numbers separated by exactly one space.
( Hint : Use an array of 100 Boolean elements, each of which indicates whether a
locker is open ( true ) or closed ( false ). Initially, all lockers are closed.)
**7.24
( Simulation: coupon collector's problem ) Coupon collector is a classic statistics
problem with many practical applications. The problem is to pick objects from
a set of objects repeatedly and find out how many picks are needed for all the
VideoNote
Coupon collector's problem
 
 
Search WWH ::




Custom Search