• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The sweet program

 
warren daniels
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5347
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
thanks, let me take a shot at it!!!


appreciated
warren
 
warren daniels
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you be able to tell me how a bitset works
and could i use it in this case.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.BitSet isn't limited to 64 bits; it's virtually limitless.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably use a circular linked list. it very closely models the 'real world' behavior.

 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Rob, you are right, because you can always add more longs to make any space available.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic