Hi everyone, I'm trying to find the solution to this problem for more than 2 hours but can't think up anything, so here's the problem:
There are X kids thinking how to split Y candies, so they play a game. They sit around in a circle (the order is increasing in clockwise). A piece of paper is given to the first kid. Whenever the kid has a piece of paper, they get 6 candies and the person next to him/her is removed from the circle. The game ends when there are no candies left or there is 1 kid left. The last kid will get the rest of the candies. Design a program that prints out the total of candies that each kid has by using a dynamic queue(singly-linked list implementation).
Example:
INPUT
Number of kids: 6
Number of candies: 100
OUTPUT
1: 12
2: 0
3: 6
4: 0
5: 82
6: 0
My approach: Using a queue, I can dequeue the first value and enqueue it again, so the second value will become the first value, then I dequeue to remove the second value. This process is repeated until the program print out the kid that has the most candies. Until now, I don't know how to print out the total candies that each kid has.
You need a separate collection that stores the number of candies that each kid has. The queue contains the indexes of the kids.
I made a few other changes:
- replaced countItems() with the much faster isEmpty()
- moved the splitCandies() login into main() because it's part of the application, not the queue class.
- Used more descriptive names.