Helpful Information
 
 
Category: Java and JSP
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.

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

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. :thumbsup:

Ermmm... In about a month, this thread will be two years old :eek:

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. :p










privacy (GDPR)