UVa10978 - Let's Play Magic!(扑克类的游戏)

来源:互联网 发布:r语言入门 知乎 编辑:程序博客网 时间:2024/06/05 16:22

You have seen a card magic trick named "Spelling Bee". The process goes as follows:

  1. The magician first arranges 13 cards in a circle, as shown in the figure below.
  2. Starting from the marked position, he counts the cards clockwisely, saying "A--C--E".
  3. He turns the card at the "E" position, and... it is an Ace!
  4. Next, he takes away the Ace and continues to count the cards, saying "T--W--O".
  5. He turns over the card at position "O"... it is a Two!!
  6. He continues to do this with the rest of the cards from Three to King. :-)

Now, how does the magician arrange the cards?

Input

Input consists of several test cases. Each case begins with an integer N (1 ≤ N ≤ 52), the number of cards to be used in the magic trick. The following N lines show the order of the turning-over of the cards and the words to be spelt. None of the words will have more than 20 characters. The format for each card is a string with two characters: first the value, and second the suit.

Input ends with a test case where N=0. This test case should not be processed.

Output

For each case, your program should output the initial arrangement of the cards.

Sample Input

13AS ACE2S TWO3S THREE4C FOUR5C FIVE6C SIX7D SEVEN8D EIGHT9D NINETH TENJH JACKQH QUEENKH KING0

Sample Output

QH 4C AS 8D KH 2S 7D 5C TH JH 3S 6C 9D

import java.io.FileInputStream;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.OutputStreamWriter;import java.io.StreamTokenizer;import java.util.Arrays;public class Main{public static final boolean DEBUG = false;public BufferedReader cin;public PrintWriter cout;public StreamTokenizer tokenizer;public int n;public Card[] cards;public boolean[] vis;static class Card{String name, spelt;}public void init(){try {if (DEBUG) {cin = new BufferedReader(new InputStreamReader(new FileInputStream("e:\\uva_in.txt")));} else {cin = new BufferedReader(new InputStreamReader(System.in));}tokenizer = new StreamTokenizer(cin);tokenizer.resetSyntax();tokenizer.wordChars('a', 'z');tokenizer.wordChars('A', 'Z');tokenizer.wordChars('0', '9');tokenizer.wordChars(128 + 32, 255);tokenizer.whitespaceChars(0, ' ');tokenizer.commentChar('/');tokenizer.quoteChar('"');tokenizer.quoteChar('\'');cout = new PrintWriter(new OutputStreamWriter(System.out));} catch (Exception e) {e.printStackTrace();}}public String next(){try {tokenizer.nextToken();if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {return String.valueOf((int)tokenizer.nval);} else return tokenizer.sval;} catch (Exception e) {e.printStackTrace();return null;}}public boolean input(){n = Integer.parseInt(next());if (n == 0) return false;cards = new Card[n];for (int i = 0; i < n; i++) {cards[i] = new Card();cards[i].name = next();cards[i].spelt = next();}return true;}public int nextPos(int cur){int i = cur;while (vis[i]) i = (i + 1) % n;return i;}public void solve(){String[] ans = new String[n];vis = new boolean[n];Arrays.fill(vis, false);int cur = 0;for (int i = 0; i < n; i++) {int len = cards[i].spelt.length();for (int j = 0; j < len; j++)  {cur = nextPos(cur);cur = (cur + 1) % n;}int prev =  (cur - 1 + n) % n;ans[prev] = cards[i].name;vis[prev] = true;}for (int i = 0; i < n; i++) {if (i != 0) cout.print(" ");cout.print(ans[i]);}cout.println();cout.flush();}public static void main(String[] args){Main solver = new Main();solver.init();while (solver.input()) {solver.solve();}}}



0 0
原创粉丝点击