UVa 170 - Clock Patience

题目:Clock Patience游戏,将52张扑克牌,按时钟依次分成13组(中心一组),每组4张全都背面向上,

            从中间组最上面一张牌開始。翻过来设为当前值,然后取当前值相应组中最上面的背过去的牌翻过来。

            取这个值为新的当前值,直到不能翻拍游戏结束。求结束时。翻过来的拍数以及最后翻过来的牌;

            假设没看明确题目详细规则,百度玩一下就明确了。

分析:模拟,数据结构(DS)。设计13个栈,模拟就可以。

说明:注意题目给的牌的顺序是逆序的,╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <stack>

using namespace std;

char Maps[] = "A23456789TJQK";

typedef struct _pnode
{
	int  value;
	char color;
	_pnode(int v, char c) {value = v; color = c;}
	_pnode(){}
}pnode;
pnode cards[54];

int value(char ch)
{
	if (ch >= '2' && ch <= '9')
		return ch-'1';
	if (ch == 'A') return 0;
	if (ch == 'T') return 9;
	if (ch == 'J') return 10;
	if (ch == 'Q') return 11;
	if (ch == 'K') return 12;
}

int main()
{
	char V,C;
	while (cin >> V && V != '#') {
		cin >> C;
		stack<pnode> Q[13];
		cards[0] = pnode(value(V), C);
		for (int i = 1; i < 52; ++ i) {
			cin >> V >> C;
			cards[i] = pnode(value(V), C);
		}
		for (int i = 51; i >= 0; -- i)
			Q[12-i%13].push(cards[i]);
		
		int   count = 0, index = 12;
		pnode now;
		while (!Q[index].empty()) {
			now = Q[index].top();
			Q[index].pop();
			index = now.value;
			++ count;
		}
		
		if (count < 10) cout << "0";
		cout << count << "," << Maps[now.value] << now.color << endl; 
	}
    return 0;
}



posted on 2017-07-25 14:06  yjbjingcha  阅读(206)  评论(0编辑  收藏  举报

导航