aspose file tools
The moose likes Beginning Java and the fly likes The sweet program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 3882
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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: 43282
    
  32
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: 20041
    
  30

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


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

Joined: Oct 02, 2003
Posts: 11811
    
  18

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43282
    
  32
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://aspose.com/file-tools
 
subject: The sweet program
 
It's not a secret anymore!