时间限制: 1 Sec 内存限制: 128 MB
题目描述
自定义结构体表示一张扑克牌,包含类型——黑桃、红桃、梅花、方块、王;大小——2,3,4,5,6,7,8,9,10,J,Q,K,A,小王(用0表示)、大王(用1表示)。输入n,输入n张扑克牌信息,从大到小输出它们的排序结果。
假设扑克牌的排序规则是大王、小王为第一大、第二大,剩余52张扑克牌按照先花色后大小排序。
花色:黑桃>红桃>梅花>方块。
大小: A>K>Q>J>>10>9>…>2。
提示:百度sort函数、strstr函数使用。
输入
测试次数t
每组测试数据两行:
第一行:n,表示输入n张扑克牌
第二行:n张扑克牌信息,格式见样例
输出
对每组测试数据,输出从大到小的排序结果
样例输入
3
5
黑桃4 红桃10 梅花Q 方块K 黑桃A
10
大王 梅花10 红桃K 方块9 黑桃2 梅花A 方块Q 小王 黑桃8 黑桃J
5
红桃K 梅花K 黑桃K 方块K 小王
样例输出
黑桃A 黑桃4 红桃10 梅花Q 方块K
大王 小王 黑桃J 黑桃8 黑桃2 红桃K 梅花A 梅花10 方块Q 方块9
小王 黑桃K 红桃K 梅花K 方块K
提示
解决方案
突然发现我之前发过这题的题解…是我很久以前写的了好像,有兴趣看的话可以直接站内搜索。
如果你试图初始化一个静态的例如某些全局变量,那么可能会得到Clang-Tidy的警告,像这样:Initialization of 'variable_name' with static storage duration may throw an exception that cannot be caught
。我不建议无视这个警告。解决方法可以是用一个静态的getter函数来包围这个变量,使得该变量的初始化时机从以前的main()
函数执行前延迟到首次调用这个getter函数。下面的get_num_size_map()
算是一个小例子,更多信息请访问引用链接。
#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>
struct Poker {
explicit Poker(const std::string &raw) {
if (raw.find("黑桃") != std::string::npos) {
this->suit = "黑桃";
} else if (raw.find("红桃") != std::string::npos) {
this->suit = "红桃";
} else if (raw.find("梅花") != std::string::npos) {
this->suit = "梅花";
} else if (raw.find("方块") != std::string::npos) {
this->suit = "方块";
} else if (raw.find("小王") != std::string::npos) {
this->suit = "小王";
this->num = "0";
return;
} else {
this->suit = "大王";
this->num = "1";
return;
}
if (raw.find("10") == std::string::npos) {
this->num = raw.back();
} else {
this->num = "10";
}
}
size_t size() const {
size_t size = 0;
size += get_suit_size_map()[this->suit];
size += get_num_size_map()[this->num];
return size;
}
bool operator<(const Poker &rhs) {
return this->size() < rhs.size();
}
friend std::ostream &operator<<(std::ostream &os, const Poker &rhs) {
os << rhs.suit;
if (!(rhs.num == "0" || rhs.num == "1")) {
os << rhs.num;
}
return os;
}
std::string suit, num;
private:
static std::map<std::string, int> get_suit_size_map() {
static const std::map<std::string, int> suit_size = {
{"大王", 5 * 13}, {"小王", 4 * 13},
{"黑桃", 3 * 13}, {"红桃", 2 * 13},
{"梅花", 1 * 13}, {"方块", 0 * 13},
};
return suit_size;
}
static std::map<std::string, int> get_num_size_map() {
static const std::map<std::string, int> num_size = {
{"2", 0}, {"3", 1}, {"4", 2}, {"5", 3},
{"6", 4}, {"7", 5}, {"8", 6}, {"9", 7},
{"10", 8}, {"J", 9}, {"Q", 10}, {"K", 11},
{"A", 12}, {"0", 0}, {"1", 0},
};
return num_size;
}
};
int main() {
size_t T;
std::cin >> T;
while (T--) {
size_t size;
std::cin >> size;
if (std::cin.get() == '\r') { std::cin.get(); }
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
std::vector<Poker> pokers{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
assert(!pokers.empty());
std::sort(pokers.begin(), pokers.end(), [](const Poker &lhs, const Poker &rhs) { return lhs.size() > rhs.size(); });
std::cout << pokers.front();
for (size_t i = 1; i < pokers.size(); ++i) {
std::cout << ' ' << pokers[i];
}
std::cout << std::endl;
}
return 0;
}
引用:
c++ – How do I iterate over the words of a string? – Stack Overflow
Initialization – cppreference.com
What is this warning in C++: initialization of ‘-‘ with static storage duration may throw an exception that cannot be caught? – Quora
c++ – How to catch exception thrown while initializing a static member – Stack Overflow
c++ – How to deal with static storage duration warnings? – Stack Overflow