题目
https://vjudge.net/problem/POJ-1786
解题思路
本题为一道模拟题,题目内容较复杂,且输出格式要求复杂严格,容易出错。本题大意即共有52张扑克牌,随后给出发牌顺序,从某一人下一位开始顺时针逐次发牌,最终需要从“S”开始从大到小依次输出每人手中的牌。题目给出了按数字和花色排序的规定,因此每人手中的牌的输出方法是唯一的。
在解题过程中,可以将花色排序和数字排序列成两个字符串,在将牌分配到对应人员手中时分别找寻花色和数字在字符串中的索引,并存入对应的结构体中,之后可以作为排序的依据。排序时可以调用系统的sort函数,按题目要求先按花色排序,再按数字排序,这一点可以体现在cmp函数中。cmp函数如下:
bool cmp(struct card a, struct card b)
{
if(a.mattc != b.mattc)
{
return a.mattc < b.mattc;
}
else
{
return a.mattn < b.mattn;
}
}
之后再注意读入输出等方面细节便可以解决问题。读入部分可以用字符类型和string类型处理,使用起来也比较方便。另外注意小心每组数据输出间还需一行空行。
具体代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<cstdlib>
#include<ctime>
#include<iomanip>
#define ll long long
#define FOR(i,a,n) for(register int i=a;i<n;i++)
#define RF(i,a,n) for(register int i=a;i>n;i--)
#define NIL -1
using namespace std;
string number = "23456789TJQKA";
string card = "CDSH";
string peo = "NESW";
struct card{
string color;
string num;
int mattc;
int mattn;
};
bool cmp(struct card a, struct card b)
{
if(a.mattc != b.mattc)
{
return a.mattc < b.mattc;
}
else
{
return a.mattn < b.mattn;
}
}
int main()
{
string p;
while(cin >> p)
{
if(p == "#")
{
break;
}
int cnts = 0,cnt[5];
memset(cnt,0,sizeof(cnt));
struct card play[5][15];
char tmp1,tmp2;
int st = (peo.find(p) + 1) % 4;
for(int i = st; cnts < 52; i = (i + 1) % 4)
{
cin >> tmp1;
cin >> tmp2;
int num = card.find(tmp1) + 1;
play[i][cnt[i]].mattc = num;
num = number.find(tmp2) + 1;
play[i][cnt[i]].mattn = num;
play[i][cnt[i]].color = tmp1;
play[i][cnt[i]].num = tmp2;
cnts++;
cnt[i]++;
}
for(int i = 0; i < 4; i++)
{
sort(play[i],play[i]+13,cmp);
}
int aaa = 0;
for(int i = 2; aaa < 4; i = (i + 1) % 4)
{
if(i == 2)
{
cout << "South player:" << endl;
}
else if(i == 3)
{
cout << "West player:" << endl;
}
else if(i == 0)
{
cout << "North player:" << endl;
}
else if(i == 1)
{
cout << "East player:" << endl;
}
cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
cout << "|";
for(int j = 0; j < 13; j++)
{
cout << play[i][j].num << " " << play[i][j].num << "|";
}
cout << endl;
cout << "|";
for(int j = 0; j < 13; j++)
{
cout << " " << play[i][j].color << " " << "|";
}
cout << endl;
cout << "|";
for(int j = 0; j < 13; j++)
{
cout << play[i][j].num << " " << play[i][j].num << "|";
}
cout << endl;
cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
aaa++;
}
cout << endl;
}
return 0;
}```