shuffle/shuffle_vanb.java

  1 
  2 import java.io.*;
  3 import java.util.*;
  4 import java.awt.geom.*;
  5 
  6 /**
  7  * Solution to Perfect Shuffle
  8  * 
  9  * @author vanb
 10  */
 11 public class shuffle_vanb
 12 {
 13     public Scanner sc;
 14     public PrintStream ps;
 15     
 16     /**
 17      * Driver.
 18      * @throws Exception
 19      */
 20     public void doit() throws Exception
 21     {
 22         sc = new Scanner( new File( "shuffle.judge" ) );
 23         ps = new PrintStream( new FileOutputStream( "shuffle.solution" ) );
 24         
 25         String cards[] = new String[1000];
 26         
 27         for(;;)
 28         {
 29             int n = sc.nextInt();
 30             if( n==0 ) break;
 31             
 32             for( int i=0; i<n; i++ )
 33             {
 34                 cards[i] = sc.next();
 35             }
 36             
 37             // Find the middle. That's where the bottom stack starts.
 38             //
 39             // If n is even it's just n/2. 
 40             // For example, if n=4, we want array indices 0 and 1 in the top stack, 2 and 3 in the bottom.
 41             // so mid = 2 = 4/2
 42             //
 43             // If n is odd, it's n/2+1, since we want the top stack to be larger.
 44             // If n=5, then we want 0,1,2 in the top stack and 3,4 in the bottom,
 45             // so mid = 3 = 5/2+1
 46             int mid = n/2 + n%2;
 47             
 48             for( int i=0; i<mid; i++ )
 49             {
 50                 // Print a card from the top stack
 51                 ps.println( cards[i] );
 52                 
 53                 // Print a card from the bottom stack, if there is one
 54                 if( mid+i<n ) ps.println( cards[mid+i] );
 55             }
 56         }
 57     }
 58     
 59     /**
 60      * @param args
 61      */
 62     public static void main( String[] args ) throws Exception
 63     {
 64         new shuffle_vanb().doit();        
 65     }   
 66 }