题解:UVA555 Bridge Hands

UVA555 Bridge Hands

题意简述

给出一堆牌,圆桌边坐有四人。现按顺时针顺序依次发牌,直到每个人手上恰有 1313 张牌。

接着每个人按照某种方式将手中的 1313 张牌进行排序(具体方式详见题目),输出每个人排序后手上的牌。

解题思路

一道不算大的模拟。

首先是模拟发牌,可以用 44vector 存储每个人手中的牌。由于是顺时针顺序,在确定了起始位置后,可以用“取模”找到下一个人的位置。

接下来是排序,可以用一个 ord 数组存储每个花色以及数值的“优先级”。这样写出排序函数比较方便。具体操作见代码。

注意:

  • 多测清空
  • 输出行末没有空格

Code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<queue>
#include<algorithm>
#include<cmath>
#include<vector>
#ifdef ONLINE_JUDGE
#define getchar getchar_unlocked
#endif
using namespace std;

inline int read() {
	char c=getchar();int x=0,f=1;
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=x*10+c-48;
	return x*f;
}

#define r(a) (a)=read()

#define cds pair<char,char>  //用两个字符描述一张牌

char Host;
string s,tmp;
vector<cds> a[5];//1234 SWNE
int now;
int ord[129];  //存储相对大小

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	ord['D']=1; ord['S']=2; ord['H']=3;
	for (int i='2';i<='9';i++)
		ord[i]=i-'2';
	ord['T']=8; ord['J']=9; ord['Q']=10; ord['K']=11; ord['A']=12;
	while (cin>>Host && Host!='#') {
		switch (Host) {
			case 'S': now=1; break;
			case 'W': now=2; break;
			case 'N': now=3; break;
			case 'E': now=4; break;
		}
		cin>>s>>tmp;
		s+=tmp;  //两行输入合并为一行
		for (int i=1;i<=4;i++) a[i].clear();  //多测清空
		for (int i=0;i<104;i++) {
			now=now%4+1;
			a[now].push_back({s[i],s[++i]});  //模拟发牌
		}
		for (int i=1;i<=4;i++)
			sort(a[i].begin(),a[i].end(),[](cds a,cds b) {
				if (a.first==b.first) return ord[a.second]<ord[b.second];
				return ord[a.first]<ord[b.first];
			});
		cout<<"S:";
		for (auto c:a[1]) cout<<" "<<c.first<<c.second; cout<<"\n";
		cout<<"W:";
		for (auto c:a[2]) cout<<" "<<c.first<<c.second; cout<<"\n";
		cout<<"N:";
		for (auto c:a[3]) cout<<" "<<c.first<<c.second; cout<<"\n";
		cout<<"E:";
		for (auto c:a[4]) cout<<" "<<c.first<<c.second; cout<<"\n";
		//先输出空格可以避免对最后一个元素的特殊处理
	}
	return 0;
}

题解:UVA555 Bridge Hands
https://pvbelln.github.io/2025/07/04/sol-uva555/
作者
PvbeLLN
发布于
2025年7月4日
许可协议