程序设计思维 C - 瑞神打牌(多关键字排序)

题目

瑞神HRZ因为疫情在家闲得无聊,同时他又非常厉害,所有的课对他来说都是水一水就能拿A + ,所以他无聊,找来了另外三个人:咕咕东,腾神以及zjm来打牌(天下苦瑞神久矣)。
显然,牌局由四个人构成,围成一圈。我们称四个方向为北 东 南 西。对应的英文是North,East,South,West。游戏一共由一副扑克,也就是52张构成。开始,我们指定一位发牌员(东南西北中的一个,用英文首字母标识)开始发牌,发牌顺序为顺时针,发牌员第一个不发自己,而是发他的下一个人(顺时针的下一个人)。这样,每个人都会拿到13张牌。
现在我们定义牌的顺序,首先,花色是(梅花) < (方片) < (黑桃) < (红桃),(输入时,我们用C, D, S, H分别表示梅花,方片,黑桃,红桃,即其单词首字母)。对于牌面的值,我们规定2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A。
现在你作为上帝,你要从小到大排序每个人手中的牌,并按照给定格式输出。(具体格式见输出描述和样例输出)。

Input
输入包含多组数据
每组数据的第一行包含一个大写字符,表示发牌员是谁。如果该字符为‘#’则表示输入结束。
接下来有两行,每行有52个字符,表示了26张牌,两行加起来一共52张牌。每张牌都由两个字符组成,第一个字符表示花色,第二个字符表示数值。

Output
输出多组数据发牌的结果,每组数据之后需要额外输出一个空行(即多组数据间要用空行来间隔,文末也需有一个空行)。
每组数据应该由24行的组成,输出按照顺时针方向,始终先输出South Player的结果,每位玩家先输出一行即玩家名称(东南西北),接下来五行,第一行和第五行输出固定格式(见样例),第二行和第四行按顺序和格式输出数值(见样例),第三行按顺序和格式输出花色(见样例)。

Sample Input
N
CTCAH8CJD4C6D9SQC7S5HAD2HJH9CKD3H6D6D7H3HQH4C5DKHKS9
SJDTS3S7S4C4CQHTSAH2D8DJSTSKS2H5D5DQDAH7C9S8C8S6C2C3
#

Sample Output
South player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 6 6 | A A | 6 6 | J J | 5 5 | 6 6 | 7 7 | 9 9 | 4 4 | 5 5 | 7 7 | 9 9 | T T|
| C | C | D | D | S | S | S | S | H | H | H | H | H |
|6 6 | A A | 6 6 | J J | 5 5 | 6 6 | 7 7 | 9 9 | 4 4 | 5 5 | 7 7 | 9 9 | T T |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
West player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 2 2 | 5 5 | 9 9 | K K | 5 5 | 7 7 | 9 9 | 4 4 | T T | J J | A A | 8 8 | A A|
| C | C | C | C | D | D | D | S | S | S | S | H | H |
|2 2 | 5 5 | 9 9 | K K | 5 5 | 7 7 | 9 9 | 4 4 | T T | J J | A A | 8 8 | A A |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
North player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 3 3 | 4 4 | J J | 2 2 | 3 3 | T T | Q Q | K K | 8 8 | Q Q | K K | 2 2 | 3 3|
| C | C | C | D | D | D | D | D | S | S | S | H | H |
|3 3 | 4 4 | J J | 2 2 | 3 3 | T T | Q Q | K K | 8 8 | Q Q | K K | 2 2 | 3 3 |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
East player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 7 7 | 8 8 | T T | Q Q | 4 4 | 8 8 | A A | 2 2 | 3 3 | 6 6 | J J | Q Q | K K|
| C | C | C | C | D | D | D | S | S | H | H | H | H |
|7 7 | 8 8 | T T | Q Q | 4 4 | 8 8 | A A | 2 2 | 3 3 | 6 6 | J J | Q Q | K K |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +

思路

此题可以用多关键字排序。
主关键字(花色):C < D < S < H
次关键字(面值):2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A
利用STL中的sort函数,编写一个合适的比较函数:
① 当花色不同时,只返回花色的比较值。
② 当花色相同时,返回面值的比较值。

代码

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <utility>
#include <algorithm>
#include <stdlib.h>
#include <string.h>

using namespace std;

// pair<char, char> p; 第一个是花色,第二个数字

int iconValue(const char c)
{
	switch (c)
	{
	case 'C':
		return 0;
	case 'D':
		return 1;
	case 'S':
		return 2;
	default:	// 'H'
		return 3;
	}
}

int numValue(const char c)
{
	if (c >= '2' && c <= '9') return c - '0';
	switch (c)
	{
	case 'T':
		return 10;
	case 'J':
		return 11;
	case 'Q':
		return 12;
	case 'K':
		return 13;
	default:	// 'A'
		return 14;
	}
}

// 先比较花色,再比较数字
bool cmp(const pair<char, char>& p1, const pair<char, char>& p2)
{
	if (p1.first != p2.first) return iconValue(p1.first) < iconValue(p2.first);
	else return numValue(p1.second) < numValue(p2.second);
}

void printCards(vector<pair<char, char>> v)
{
	cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
	cout << "|";
	for (vector<pair<char, char>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << (*it).second << " " << (*it).second << "|";
	}
	cout << endl;
	cout << "|";
	for (vector<pair<char, char>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << " " << (*it).first << " " << "|";
	}
	cout << endl;
	cout << "|";
	for (vector<pair<char, char>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << (*it).second << " " << (*it).second << "|";
	}
	cout << endl;
	cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
}

void print(vector<int> v)
{
	vector<int>::iterator i1 = v.begin();
	vector<int>::iterator i2 = v.begin(); i2++;
	while (i2 != v.end()) {
		cout << *i1 << " ";
		i1++; i2++;
	}
	cout << *i1 << endl;
}

int main()
{
	char pos;
	cin >> pos;

	while (pos != '#') {

		string order;

		if (pos == 'N') order = "4123";
		else if (pos == 'E') order = "1234";
		else if (pos == 'S') order = "2341";
		else order = "3412";

		string print_order[] = { "South player:","West player:","North player:" ,"East player:" };

		vector<pair<char, char>> v[4];

		for (int i = 0; i < 13; i++) {
			for (int j = 0; j < 4; j++) {
				char icon, num;
				cin >> icon >> num;
				pair<char, char> p(icon, num);
				v[j].push_back(p);
			}
		}

		for (int i = 0; i < 4; i++) {
			sort(v[i].begin(), v[i].end(), cmp);
		}

		cout << "South player:" << endl;
		int index = order.find(1 + '0');
		printCards(v[index]);

		cout << "West player:" << endl;
		index = order.find(2 + '0');
		printCards(v[index]);

		cout << "North player:" << endl;
		index = order.find(3 + '0');
		printCards(v[index]);

		cout << "East player:" << endl;
		index = order.find(4 + '0');
		printCards(v[index]);

		cout << endl;

		cin >> pos;
	}
}

End

发布了5 篇原创文章 · 获赞 0 · 访问量 47

猜你喜欢

转载自blog.csdn.net/weixin_43826681/article/details/104690382
今日推荐