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

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

13
AS ACE
2S TWO
3S THREE
4C FOUR
5C FIVE
6C SIX
7D SEVEN
8D EIGHT
9D NINE
TH TEN
JH JACK
QH QUEEN
KH KING
0

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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值