Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Help with java

  1. #1
    psp
    psp is offline
    New Coder
    Join Date
    Mar 2003
    Location
    CA
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with java

    Hello,
    I am trying to write java code for "perfect shuffle".
    Perfect shuffle is :given Ncards,you cut the cards at say ICards.Take the bottom card from the top portion first and then bottom card from bottom portion and so on.Need to calculate how many such shuffles are needed to restore it the original order.

    Can somebody give me an idea of what data structures to use.How to proceed?

    Thanks a ton.

  • #2
    New Coder
    Join Date
    Jun 2002
    Location
    Caerdydd
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The best class to use to model a pack of cards in this way is the Stack class. It's a bit like a Vector or ArrayList in that you can bung any old object into a Stack, but you're only allowed to add or remove objects from the start of the list with the push() and pop() methods.

    The way I'd tackle your problem is to split your pack of cards into two Stacks and then run through a loop to add cards to a third Stack which will be your recombined deck. If the loop counter is even then add from the first stack else add from the second until both piles are empty (or 52 insertions, they should be the same). At the end of this loop you can compare the combined Stack with your original deck to see if they match - if not then repeat the shuffle.

    If you're splitting the deck exactly in half each time, then I'm told it should be back in it's original state after eight shuffles although that seems a bit low to me.

    BL
    "nam et ipsa scientia potentas est" - Francis Bacon

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually, i did it in C. not sure if u wanted it so i din post the code out....if u do, then gv me a buzz..maybe u can see the sturcture there and convert it to java.
    Warm Regards,
    Mivec

  • #4
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ermmm... In about a month, this thread will be two years old

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Yeah if you are going to try and help someone cheat on their homework assignments then you might need to be a little quicker than that.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •