题意:
四人打牌,共52张牌,其中四种花色大小:(梅花)<(方片)<(黑桃)<(红桃),分别用C,D,S,H表示;13种牌面大小:2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A。
四人分别对应North,East,South,West,发牌人从自己顺时针第一个人开始顺时针发牌,要求把各人手中的牌从小到大排序并输出。
输入要求:
包含多组数据,若每组数据开始时输入‘N’,‘E’,‘S’,‘W’中的一个,则代表发牌人,若第一个输入‘#’,则表示输入结束,退出程序。
接下来将发牌前牌的顺序输入:分两行,每行52个字符,两个字符(花色+牌面大小)表示一张牌。
输入示例如下:
N
CTCAH8CJD4C6D9SQC7S5HAD2HJH9CKD3H6D6D7H3HQH4C5DKHKS9
SJDTS3S7S4C4CQHTSAH2D8DJSTSKS2H5D5DQDAH7C9S8C8S6C2C3
#
输出要求:
1、以south、west、north、east的顺序输出各人的牌面;
2、每组数据应该由24行的组成:先输出玩家名称,再按照以下样例的格式输出牌面。
输出样例如下:
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|
+---+---+---+---+---+---+---+---+---+---+---+---+---+
思路:
这题,首先需要声明一个结构体card,来存储每张牌的花色以及牌面大小,在对card进行大于小于符号操作符重载时,开始是打算使用map,将‘C’,‘D’,‘S’,‘H’分别和1,2,3,4相对应,将‘2’-‘A’分别和 ‘1’~‘13’对应,可以较为方便的进行操作符重载,但是,vj上这题不能使用C++11,所以无法使用直接初始化map的方式进行操作,最后妥协于使用笨办法一个一个进行比较。
在完成操作符重载之后,别的操作就比较简单,用vector对每个人的牌进行存储,使用algorithm中的sort函数对vector进行排序,最后进行输出。
代码:
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
struct card {
char color;
char size;
card(){}
card(char _color, char _size) {
color = _color;
size = _size;
}
bool operator>(const card &c) const{
if (color != c.color) {
if ((color == 'C' || color == 'D' || color == 'S') && (c.color == 'C' || c.color == 'D' || c.color == 'S'))
return (color - 'A') > (c.color - 'A');
if (color == 'H')
return true;
if (c.color == 'H')
return false;
}
else {
if (size >= '2'&&size <= '9'&&c.size >= '2'&&c.size <= '9')
return (size - '2') > (c.size - '2');
if (size >= '2'&&size <= '9'&&c.size > '9')
return false;
if (size > '9'&&c.size <= '9')
return true;
if (size > '9'&&c.size > '9') {
if (size == 'A'
|| (size == 'K'&&(c.size=='Q'||c.size=='J'||c.size=='T'))
|| (size == 'Q'&&(c.size == 'J' || c.size == 'T'))
|| (size == 'J'&&c.size=='T'))
return true;
else
return false;
}
}
}
bool operator<(const card &c) const {
if (color != c.color) {
if ((color == 'C' || color == 'D' || color == 'S') && (c.color == 'C' || c.color == 'D' || c.color == 'S'))
return (color - 'A') < (c.color - 'A');
if (color == 'H')
return false;
if (c.color == 'H')
return true;
}
else {
if (size >= '2'&&size <= '9'&&c.size >= '2'&&c.size <= '9')
return (size - '2') < (c.size - '2');
if (size >= '2'&&size <= '9'&&c.size > '9')
return true;
if (size > '9'&&c.size <= '9')
return false;
if (size > '9'&&c.size > '9') {
if (size == 'A'
|| (size == 'K' && (c.size == 'Q' || c.size == 'J' || c.size == 'T'))
|| (size == 'Q' && (c.size == 'J' || c.size == 'T'))
|| (size == 'J'&&c.size == 'T'))
return false;
else
return true;
}
}
}
};
vector<card> player[4];//0-3:S-W-N-E
int main()
{
char croupier;
while (1) {
cin >> croupier;
if (croupier == '#')
break;
char pai1[104];
for (int i = 0; i < 104; i++) {
cin >> pai1[i];
}
int j;
switch (croupier)
{
case 'S': j = 0;break;
case 'W': j = 1;break;
case 'N': j = 2;break;
case 'E': j = 3;break;
default:
break;
}
for (int i = 0; i < 52; i++) {//发牌
player[(i + j + 1) % 4].push_back(card(pai1[2 * i], pai1[2 * i+1]));
}
for (int i = 0; i < 4; i++) {
if (i == 0)cout << "South ";
if (i == 1)cout << "West ";
if (i == 2)cout << "North ";
if (i == 3)cout << "East ";
cout << "player:\n";
sort(player[i].begin(), player[i].end());
cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
for (int j = 0; j < 13; j++) {
cout << "|" << player[i][j].size << " " << player[i][j].size;
}
cout << "|" << endl;
for (int j = 0; j < 13; j++) {
cout << "| " << player[i][j].color << " " ;
}
cout << "|" << endl;
for (int j = 0; j < 13; j++) {
cout << "|" << player[i][j].size << " " << player[i][j].size;
}
cout << "|" << endl;
cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
}cout << endl;
for (int i = 0; i < 4; i++)
player[i].clear();
}
}