This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes The sweet program Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "The sweet program" Watch "The sweet program" New topic
Author

The sweet program

warren daniels
Greenhorn

Joined: Oct 07, 2011
Posts: 3
hi

im new!!!


my problem:
n children numbered 1 to n are sitting in a circle. starting at child 1, a sweet is passed. after m passes the child holding the sweet is eliminated. if child x gets eliminated he gives the sweet to child x+1 and leaves the ring. that does not count far a pass. the children in the circle close ranks and the game continues with the child who was sitting after the eliminated child,taking the sweet. assume m is constant for each elimination.
write a program that will determine which child would get the sweet in the end.

I'm totally confused please help!!!
what sort of structure would i use and how?

regards
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3065
    
    1

Well, you could keep a list of numbers -- one for each child -- and also have a variable which keeps track of the index of the last child removed. You remove element with index (i+m) mod size, and repeat until the size of the list is 1.

And welcome to CodeRanch! Please KeepItDown.
warren daniels
Greenhorn

Joined: Oct 07, 2011
Posts: 3
ok
thanks, let me take a shot at it!!!


appreciated
warren
warren daniels
Greenhorn

Joined: Oct 07, 2011
Posts: 3
Would you be able to tell me how a bitset works
and could i use it in this case.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
A bitset squeezes 64 true/false values into the space of a long integer (64 bits). It is a bit like a boolean[] with 64 elements. If you want to change the size of the array whenever a child picks a sweet, the List might be a better bet.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

java.util.BitSet isn't limited to 64 bits; it's virtually limitless.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

I would probably use a circular linked list. it very closely models the 'real world' behavior.


Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Yes, Rob, you are right, because you can always add more longs to make any space available.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
I wouldn't use numbers to represent children, or any kind of clever math calculations. I would use objects to represent the things in the problem. Thus a Child class that has an instance of a Sweet class that can be null. I like Fred's suggestion, though I personally find ArrayList easier to use so I would do that.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: The sweet program
 
Similar Threads
Can't get to next question...
Break back to Topic Menu
to read intgers into program
Help with math
Using THIS