湖南大学21夏训练三4.打牌

【问题描述】

牌只有1到9,手里拿着已经排好序的牌a,对方出牌b,用程序判断手中牌是否能够压过对方出牌。
规则:出牌牌型有5种
[1]一张 如4 则5…9可压过
[2]两张 如44 则55,66,77,…,99可压过
[3]三张 如444 规则如[2]
[4]四张 如4444 规则如[2]
[5]五张 牌型只有12345 23456 34567 45678 56789五个,后面的比前面的均大。

【输入形式】

输入有多行,第一行代表手中的牌,长度不超过200个数字。接下来的每一行代表每次对方出的牌。

【输出形式】

输出有多行,代表手中的牌是否能压过对方出的牌,压过输出YES, 并列出所有可选项,可选项之间用空格分隔。 否则输出NO。

【样例输入】

17624234556367
33
222
34567
【样例输出】

YES 44 55 66 77
YES 666
NO

【思路分析】
题目不难,但很麻烦。
前四个条件思路一致,用set存储并计算每个元素个数,比较输出即可
最后一个条件穷举即可

#include<iostream>
#include<string>
#include<set>
using namespace std;
string Mysort(string temp){
	int len=temp.length();
	char ch;
	for(int i=0;i<len;i++)
	for(int j=i;j<len;j++){
		if(temp[j]<temp[i]){
			ch=temp[j];
			temp[j]=temp[i];
			temp[i]=ch;
		}
	}
	return temp;
}
int main(){
	string str;
	cin>>str;
	set<char>s;
	for(unsigned int i=0;i<str.length();i++)
	s.insert(str[i]);
	str=Mysort(str);
	set<char>::iterator it;
	int a[10001];
	int count=0;
	for(int i=0;i<10001;i++)
	a[i]=0;
	for(it=s.begin();it!=s.end();it++){
		for(unsigned int i=0;i<str.length();i++)
			if(str[i]==*it)
			a[count]++;
			count++;
	}
	string temp,str1;
	for(int i=0;i<count;i++)
	str1+='1';
	int x=0;
	for(it=s.begin();it!=s.end();it++){
		str1[x]=*it;
		x++;
	}
	while(cin>>temp){
		int len=temp.length();
		if(len==1){
			int shu1=0;
		for(it=s.begin();it!=s.end();it++)
		if(*it>temp[0]){
	    cout<<"YES ";
	    shu1++;
	    break;
		}
		if(shu1==0){
			cout<<"NO"<<endl;
			continue;
		}
		for(it=s.begin();it!=s.end();it++){
			if(*it>temp[0])
			cout<<*it<<" ";
		}
		if(shu1!=0)
			cout<<endl;
				}
		if(len==2){
			int shu2=0;
			count=0;
		for(it=s.begin();it!=s.end();it++){
	   if(*it>temp[0]&&a[count]>=2){
	    cout<<"YES ";
	    shu2++;
	    break;
		}
		count++;
			}
		if(shu2==0){
			cout<<"NO"<<endl;
			continue;
		}
		count=0;
		for(it=s.begin();it!=s.end();it++){
			if(*it>temp[0]&&a[count]>=2)
			cout<<*it<<*it<<" ";
			count++;
		}
		if(shu2!=0)
			cout<<endl;
		}	
		if(len==3){
			int shu3=0;
			count=0;
		for(it=s.begin();it!=s.end();it++){
	   if(*it>temp[0]&&a[count]>=3){
	    cout<<"YES ";
	    shu3++;
	    break;
		}
		count++;
			}
		if(shu3==0){
			cout<<"NO"<<endl;
			continue;
		}
		count=0;
		for(it=s.begin();it!=s.end();it++){
			if(*it>temp[0]&&a[count]>=3)
			cout<<*it<<*it<<*it<<" ";
			count++;
		}
		if(shu3!=0)
			cout<<endl;
		}
		if(len==4){
			int shu4=0;
			count=0;
		for(it=s.begin();it!=s.end();it++){
	   if(*it>temp[0]&&a[count]>=4){
	    cout<<"YES ";
	    shu4++;
	    break;
		}
		count++;
			}
		if(shu4==0){
			cout<<"NO"<<endl;
			continue;
		}
		count=0;
		for(it=s.begin();it!=s.end();it++){
			if(*it>temp[0]&&a[count]>=4)
			cout<<*it<<*it<<*it<<*it<<" ";
			count++;
		}
		if(shu4!=0)
			cout<<endl;
		}
		if(len==5){
			int shu5=0;
			if(temp=="12345"){
				if(str1.find("23456")!=string::npos||
				str1.find("34567")!=string::npos||
				str1.find("45678")!=string::npos||
				str1.find("56789")!=string::npos)
				{
				cout<<"YES ";
				shu5++;
				}
				
				if(str1.find("23456")!=string::npos)
				cout<<"23456 ";
				if(str1.find("34567")!=string::npos)
				cout<<"34567 ";
				if(str1.find("45678")!=string::npos)
				cout<<"45678 ";
				if(str1.find("56789")!=string::npos)
				cout<<"56789 ";
		}
			if(temp=="23456"){
				if(str1.find("34567")!=string::npos||
				str1.find("45678")!=string::npos||
				str1.find("56789")!=string::npos)
				{
				cout<<"YES ";
				shu5++;
				}
				if(str1.find("34567")!=string::npos)
				cout<<"34567 ";
				if(str1.find("45678")!=string::npos)
				cout<<"45678 ";
				if(str1.find("56789")!=string::npos)
				cout<<"56789 ";
			}
			if(temp=="34567"){
				if(str1.find("45678")!=string::npos||
				str1.find("56789")!=string::npos)
				{
				cout<<"YES ";
				shu5++;
				}
				if(str1.find("45678")!=string::npos)
				cout<<"45678 ";
				if(str1.find("56789")!=string::npos)
				cout<<"56789 ";
     			}
			if(temp=="45678"){
				if(str1.find("45678")!=string::npos)
				{
				cout<<"YES ";
				shu5++;
				}
				if(str1.find("56789")!=string::npos)
				cout<<"56789 ";
			}
			if(temp=="56789")
			cout<<"NO",shu5++; 
			if(shu5!=0)
			cout<<endl;
			if(shu5==0)
			cout<<"NO"<<endl;
		}
	}
	return 0;
} 
好的,以下是用Python实现创建一副54张扑克、洗和分的示例代码: ```python import random # 创建一副54张扑克 suits = ['♠', '♥', '♣', '♦'] ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] deck = [rank + suit for suit in suits for rank in ranks] + ['小王', '大王'] # 洗(打乱的顺序) random.shuffle(deck) # 打牌(分) player1 = deck[:17] player2 = deck[17:34] player3 = deck[34:51] bottom_cards = deck[51:] print('玩家1的手是:', player1) print('玩家2的手是:', player2) print('玩家3的手是:', player3) print('底是:', bottom_cards) ``` 解释一下代码: 1. 首先定义了两个列表`suits`和`ranks`,分别表示的花色和点数,然后使用列表推导式生成一副54张扑克,最后添加了两张大小王。 2. 使用Python的内置函数`random.shuffle()`对进行洗,打乱的顺序。 3. 使用切片操作将分给个玩家和底。 运行上述代码,输出如下: ``` 玩家1的手是: ['2♥', '8♥', '7♦', '5♠', '3♠', '6♠', 'A♥', '10♠', '3♥', '5♦', '4♠', '10♣', 'K♦', 'J♠', '4♦', '2♠', '7♠'] 玩家2的手是: ['8♣', '10♦', '6♣', 'Q♥', 'K♣', '5♥', '7♥', '6♥', '9♣', 'Q♦', 'A♦', '9♥', 'K♥', 'J♣', 'A♠', '2♦', 'Q♣'] 玩家3的手是: ['J♥', '8♠', '3♦', '4♣', '7♣', '9♠', '4♥', 'K♠', '6♦', '10♥', '5♣', '9♦', 'Q♠', 'A♣', 'J♦', '2♣', '3♣'] 底是: ['大王', '小王', '8♦'] ``` 可以看到,已经成功地被分给了个玩家和底
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值