小丑牌|超级大模拟
2024-10-09 21:45:07
发布于:美国
42阅读
0回复
0点赞
T4 - 小丑牌
题目链接跳转:点击跳转
解题思路
也是一道模拟题,但需要注意以下几点:
- 同一个点数可能会出现五次,那么此时应该输出 High Card(如题意)。
- 如果有一个牌型符合上述多条描述,请输出符合描述的牌型中在规则中最后描述的牌型。
- 牌的数量不局限于传统的扑克牌,每张牌可以题目中四种花色的任意之一。
代码实现
本题的 C++ 代码如下:
#include <iostream>
#include <algorithm>
using namespace std;
int t;
struct card{
int rank;
string suit;
} cards[10];
bool cmp(card a, card b){
return a.rank < b.rank;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
for (int i=1; i<=5; i++){
string rank; string suit;
cin >> rank >> suit;
int act;
if (rank == "J") act = 11;
else if (rank == "Q") act = 12;
else if (rank == "K") act = 13;
else if (rank == "A") act = 14;
else if (rank == "10") act = 10;
else act = rank[0] - '0';
cards[i] = (card){act, suit};
}
sort(cards+1, cards+6, cmp);
int cnt[20] = {};
bool isSameSuit = true;
bool isRanked = true;
int pairs = 0; int greatest = 0;
for (int i=1; i<=5; i++){
cnt[cards[i].rank] += 1;
if (i > 1 && cards[i].suit != cards[i-1].suit)
isSameSuit = false;
if (i > 1 && cards[i-1].rank + 1 != cards[i].rank)
isRanked = false;
}
for (int i=1; i<=15; i++){
if (cnt[i] == 2) pairs++;
greatest = max(greatest, cnt[i]);
}
if (isRanked && isSameSuit){
if (cards[5].rank == 14) cout << "Royal Flush" << endl;
else cout << "Straight Flush" << endl;
} else if (isRanked) cout << "Straight" << endl;
else if (pairs == 1 && greatest == 3) cout << "Full House" << endl;
else if (greatest == 4) cout << "Four of a Kind" << endl;
else if (greatest == 3) cout << "Three of a Kind" << endl;
else if (pairs == 2) cout << "Two Pairs" << endl;
else if (pairs == 1) cout << "One Pair" << endl;
else cout << "High Card" << endl;
}
return 0;
}
全部评论 1
顶!
2024-10-09 来自 美国
0
有帮助,赞一个