solution
题目意思题面里说的很清楚了,这里就不再赘述。
说一个坑,就是这句话
当发现四条命都死了以后,统计现在每堆牌上边正面朝上的牌的数目
这个地方想要表达的是只要是正面向上的牌都统计,而不是只统计每堆顶上的牌。
然后这个题,我们可以发现她的要求若想实现,那么不仅要实现“插入队头”,还要实现“删去队尾”“删去队头”等操作。这个可以用链表维护,但是更简单且行之有效的方法是使用 STL 里的双端队列 deque 。
由于这是个细节较多的模拟题,直接来看代码。细节在代码中说。
#include <iostream>
#include <cstdio>
#include <queue>
#include <queue>
#include <deque>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
// 第一关键字存放具体的牌,第二关键字表示是否正面向上
deque<PII > q[105];
void ctn(char c, int i, int j)
{
if (c == '0') q[i].push_back({10, 0});
else if (c == 'A') q[i].push_back({1, 0});
else if (c == 'J') q[i].push_back({11, 0});
else if (c == 'Q') q[i].push_back({12, 0});
else if (c == 'K') q[i].push_back({13, 0});
}
int main()
{
for (int i = 1; i <= 13; i++)
for (int j = 1; j <= 4; j++)
{
char ch; cin >> ch;
if (ch >= '2' && ch <= '9') q[i].push_back({ch - '0', 0});
else ctn(ch, i, j);
}
int level = 4; // 生命值
int d = 13; // 当前所在的牌堆
int step = 1; // 执行哪种操作(生命牌、非生命牌)
while (level)
{
int p; if (step == 1) p = q[d].front().first; else p = q[d].back().first;
if (step == 1) q[d].pop_front();
else q[d].pop_back();
// 两种取牌的方式
d = p;
if (p == 13) {--level; step = 1;} // 遇到 K
else
{
q[d].push_front({p, 1});
step = 2;
}
}
int cnt[15] = {}, ans = 0;
for (int i = 1; i <= 13; i++)
{
while (!q[i].empty()) // ! 每一个正面向上的都要统计
{
int v = q[i].front().first, c = q[i].front().second;
if (v != 13 && c) cnt[v]++;
q[i].pop_front();
}
}
for (int i = 1; i <= 14; i++)
if (cnt[i] == 4) ++ans;
cout << ans << "\n";
return 0;
}