n an ancient land, the beautiful princess Eve (or handsome prince Val) had many suitors. Being royalty, it was decided that a special process must be used to determine which suitor would win the hand of the prince/princess. First, all of the suitors would be lined up one after the other and assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at 4 the suitor in the first position, she/he would then count three suitors down the line (because of the three letters in his/her name) and that suitor would be eliminated and removed from the line. The prince/princess would then continue, counting three more suitors, and eliminate every third suitor. When the end of the line is reached, counting would continue from the beginning. For example, if there were 6 suitors, the elimination process would proceed as follows: 123456 initial list of suitors, starting count from 1 12456 suitor 3 eliminated, continue counting from 4 1245 suitor 6 eliminated, continue counting from 1 125 suitor 4 eliminated, continue counting from 5 15 suitor 2 eliminated, continue counting from 5 1 suitor 5 eliminated, 1 is the lucky winner A Solution Using Queues Write a program/method called findPlaceToStand (int n) that uses a queue to determine the positions that you should stand in to marry the prince/princess if there are n suitors. When counting, traverse the queue. For the first and second suitors (numbers) on the queue, put them at the back of the queue. For the third suitor, discard the number. When there is only one number left, the suitor is determined. In the driver, find the commented out code near the bottom of main() to get started. The last two lines invoke the function findPlaceToStand() This takes an integer and determines where to stand to win in the game of love. Should you use a List, a Stack, or a Queue to solve this? Does the selection of structure affect your results? Use the Queue interface, but allocate each queue as a LinkedList or use your own Linked List implementation, if finished: Queue myQueue = new LinkedList(); Both Queue and LinkedList are defined in java.util.
n an ancient land, the beautiful princess Eve (or handsome prince Val) had many suitors. Being royalty, it was decided that a special process must be used to determine which suitor would win the hand of the prince/princess. First, all of the suitors would be lined up one after the other and assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at 4 the suitor in the first position, she/he would then count three suitors down the line (because of the three letters in his/her name) and that suitor would be eliminated and removed from the line. The prince/princess would then continue, counting three more suitors, and eliminate every third suitor. When the end of the line is reached, counting would continue from the beginning.
For example, if there were 6 suitors, the elimination process would proceed as follows:
123456 initial list of suitors, starting count from 1
12456 suitor 3 eliminated, continue counting from 4
1245 suitor 6 eliminated, continue counting from 1
125 suitor 4 eliminated, continue counting from 5
15 suitor 2 eliminated, continue counting from 5
1 suitor 5 eliminated, 1 is the lucky winner
A Solution Using Queues
Write a program/method called findPlaceToStand (int n) that uses a queue to determine the positions that you should stand in to marry the prince/princess if there are n suitors. When counting, traverse the queue. For the first and second suitors (numbers) on the queue, put them at the back of the queue. For the third suitor, discard the number. When there is only one number left, the suitor is determined.
- In the driver, find the commented out code near the bottom of main() to get started.
- The last two lines invoke the function findPlaceToStand()
- This takes an integer and determines where to stand to win in the game of love.
- Should you use a List, a Stack, or a Queue to solve this?
- Does the selection of structure affect your results?
- Should you use a List, a Stack, or a Queue to solve this?
- This takes an integer and determines where to stand to win in the game of love.
Use the Queue interface, but allocate each queue as a LinkedList or use your own Linked List implementation, if finished:
Queue<Integer> myQueue = new LinkedList<Integer>();
Both Queue and LinkedList are defined in java.util.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images