UVA 10205 – Stack ’em Up

Pretty much just an implementation problem.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P10205 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
    String line = null;
    StringTokenizer st = null;

    int cases = Integer.parseInt(br.readLine());
    br.readLine(); // Read blank line.

    for (int zz = 0; zz < cases; zz++) {
      if (zz != 0)
        out.println();

      int shuffles = Integer.parseInt(br.readLine());
      int[][] caesar = new int[shuffles][52];
      for (int i = 0; i < shuffles; i++) {
        int count = 0;
        while (count < 52) {
          st = new StringTokenizer(br.readLine());
          while (st.hasMoreTokens()) {
            caesar[i][count] = Integer.parseInt(st.nextToken()) - 1;
            count++;
          }
        }
      }

      Card[] newDeck = new Card[52];
      init(newDeck);
      while ((line = br.readLine()) != null) {
        if (line.trim().equals(""))
          break;
        int move = Integer.parseInt(line) - 1;
        Card[] tmp = new Card[52];
        for (int i = 0; i < 52; i++) {
          tmp[i] = newDeck[caesar[move][i]];
        }
        newDeck = tmp;
      }
      for (int i = 0; i < 52; i++) {
        out.println(newDeck[i]);
      }
    }

    out.close();
    br.close();
  }

  private static void init(Card[] newDeck) {
    String[] suitOrder = new String[] { "Clubs", "Diamonds", "Hearts", "Spades" };
    String[] order = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
    for (int j = 0; j < 4; j++) {
      for (int i = 0; i < 13; i++) {
        newDeck[i + 13 * j] = new Card(order[i], suitOrder[j]);
      }
    }
  }

  @SuppressWarnings("unused")
  private static class Card {
    String card;
    String card2;
    String num;
    String suit;

    public Card(String num, String suit) {
      card2 = num + " of " + suit;
      this.num = num;
      this.suit = suit;
      if (this.num == "10") {
        card = "T" + suit.charAt(0);
      } else {
        card = num.charAt(0) + "" + suit.charAt(0);
      }
    }

    public String toString() {
      return this.card2;
    }
  }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.