问题:
瑞神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 intput:
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|
±–±--±–±--±–±--±–±--±–±--±–±--±–+
题解:
- 大体思路
这个题就是一个排序问题,不过需要我们对于字符进行处理,将字符串按照其权的大小对应一个数字,进行排序。这就需要用到map和优先级队列。我们将每一张卡片看成是一个结构体,将花色和大小记录并重载<进行牌的大小的判断。之后就按照发牌的顺序将牌填入队列中。之后按照格式输出就可。
#include <iostream>
#include <queue>
#include <map>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
map<char, int, less<char>, allocator<pair<const char,int> > > mp1;
struct card{
char hua;
char pai;
card(char x,char y):hua(x),pai(y){}
bool operator < (const card& b)const{
if(hua!=b.hua) return mp1[hua]<mp1[b.hua];
else return mp1[pai]<mp1[b.pai];
}
};
int main() {
mp1['C']=4;
mp1['D']=3;
mp1['S']=2;
mp1['H']=1;
mp1['2']=17;
mp1['3']=16;
mp1['4']=15;
mp1['5']=14;
mp1['6']=13;
mp1['7']=12;
mp1['8']=11;
mp1['9']=10;
mp1['T']=9;
mp1['J']=8;
mp1['Q']=7;
mp1['K']=6;
mp1['A']=5;
char starter;
while (true) {
cin>>starter;
if(starter=='#')break;
priority_queue<card> temp1[4];//0:n 1:e 2:s 3:w
priority_queue<card> temp2[4];//0:n 1:e 2:s 3:w
priority_queue<card> temp3[4];//0:n 1:e 2:s 3:w
int s;
if(starter=='N')s=1;
if(starter=='E')s=2;
if(starter=='S')s=3;
if(starter=='W')s=0;
for (int i=0; i<52; i++) {
char c1,c2;
cin>>c1>>c2;
temp1[s].push(card(c1, c2));
temp2[s].push(card(c1, c2));
temp3[s].push(card(c1, c2));
s++;
s=s%4;
}
cout<<"South player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp1[2].top().pai<<" "<<temp1[2].top().pai;
temp1[2].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<" "<<temp2[2].top().hua<<" ";
temp2[2].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp3[2].top().pai<<" "<<temp3[2].top().pai;
temp3[2].pop();
}
cout<<'|'<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<"West player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp1[3].top().pai<<" "<<temp1[3].top().pai;
temp1[3].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<" "<<temp2[3].top().hua<<" ";
temp2[3].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp3[3].top().pai<<" "<<temp3[3].top().pai;
temp3[3].pop();
}
cout<<'|'<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<"North player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp1[0].top().pai<<" "<<temp1[0].top().pai;
temp1[0].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<" "<<temp2[0].top().hua<<" ";
temp2[0].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp3[0].top().pai<<" "<<temp3[0].top().pai;
temp3[0].pop();
}
cout<<'|'<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<"East player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp1[1].top().pai<<" "<<temp1[1].top().pai;
temp1[1].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<" "<<temp2[1].top().hua<<" ";
temp2[1].pop();
}
cout<<'|'<<endl;
for (int j=0; j<13; j++) {
cout<<'|'<<temp3[1].top().pai<<" "<<temp3[1].top().pai;
temp3[1].pop();
}
cout<<'|'<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl<<endl;
}
return 0;
}