POJ-1786 Bridge Hands

题目

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;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值