This is my first post!
Anyway, I know this is a pretty simple problem, but I can't figure out how to read in the input correctly. I'm pretty confident that my algorithm works. Because I can't always read in the input correctly, I'm having problems with a few other problems too. I'd appreciate it if someone could help me out on this!
[java]
import java.io.*;
import java.util.*;
class Main {
// utility function to read from stdin
static String readLn (int maxLg) {
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg)).trim();
} //end of readLn
// entry point from OS
public static void main (String args[]) {
Main myWork = new Main(); // create a dinamic instance
myWork.begin(); // the true entry point
} //end of main
public static final int CARD_SIZE = 53;
public static final int BIG_SIZE = 100000;
void begin() {
String input;
StringTokenizer idata;
int number_of_cases;
int number_of_shuffles;
int[][] shuffles;
int shuffle_num;
int[] cards;
/*
//Testing code for unrank()
for( int i = 1; i <= 52; i++ ) {
System.out.println( unrank( i ) );
}
*/
idata = new StringTokenizer( Main.readLn( 255 ) );
number_of_cases = Integer.parseInt( idata.nextToken() );
Main.readLn( 255 );
for( int case_num = 0; case_num < number_of_cases; case_num++ ) {
idata = new StringTokenizer( Main.readLn( 255 ) );
number_of_shuffles = Integer.parseInt( idata.nextToken() );
cards = new int[ CARD_SIZE ];
for( int i = 1; i <= 52; i++ )
cards[ i ] = i;
//NOTE: The first shuffle starts from index 1, and the first real data starts from index 1
shuffles = new int[ number_of_shuffles+1 ][ CARD_SIZE ];
idata = new StringTokenizer( Main.readLn( BIG_SIZE ) );
for( int i = 1; i <= number_of_shuffles; i++ ) {
for( int j = 1; j <= 52; j++ )
shuffles[ i ][ j ] = Integer.parseInt (idata.nextToken());
} //end of for i
while( true ) {
input = Main.readLn( 255 );
idata = new StringTokenizer( input );
if( !idata.hasMoreTokens() )
break;
shuffle_num = Integer.parseInt (idata.nextToken());
int[] new_cards = new int[ CARD_SIZE ];
for( int i = 1; i <= 52; i++ )
new_cards[ i ] = cards[ shuffles[ shuffle_num ][ i ] ];
cards = new_cards;
} //end of while
for( int i = 1; i <= 52; i++ )
System.out.println( unrank( cards[ i ] ) );
System.out.println();
} //end of for case
} //end of begin
/** the ranks go from 1 to 52 inclusive */
String unrank( int rank ) {
String result = "";
int number = (rank + 1) % 13;
switch( number ) {
case 2: case 3: case 4: case 5: case 6:
case 7: case 8: case 9: case 10:
result += number;
break;
case 11:
result = "Jack";
break;
case 12:
result = "Queen";
break;
case 0:
result = "King";
break;
case 1:
result = "Ace";
break;
default:
System.out.println( "error in unrank's first switch" );
break;
} //end of switch
result += " of ";
switch( (rank-1) / 13 ) {
case 0:
result += "Clubs";
break;
case 1:
result += "Diamonds";
break;
case 2:
result += "Hearts";
break;
case 3:
result += "Spades";
break;
default:
System.out.println( "error in unrank's second switch" );
break;
} //end of switch
return result;
} //end of unrank
} //end of Main
[/java]