Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 07:50 PM
Member
 
Join Date: Jul 2007
Posts: 40
trill is on a distinguished road
Help with code "perfect shuffle"
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 09:25 AM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
failure at Class.forName("oracle.jdbc.driver.OracleDriver"); RonNYC Eclipse 1 03-14-2008 04:51 PM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 09:53 AM
Exception in thread "main" java.net.ConnectException: Connection timed out osval Advanced Java 1 07-28-2007 12:59 AM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-26-2007 12:55 AM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 08:32 AM


All times are GMT +3. The time now is 10:13 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org