跳到正文

算法·理论

9.8--deque--T4

shifuanOwO约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年9月10日
收录
2026年9月10日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

T4

题意

0代表10,J代表11,Q代表12,K代表13,A代表1,剩下的就是数字
步骤:

  1. 先将扑克牌(不含大小王)平均分成13堆,每堆4张(题目给定)
  2. 把第13堆牌的第一张拿起放到牌上的数字所对应编号的堆的最上边。(例如抽到 2,正面朝上放到第2堆牌最上面,又比如抽到 J,放到第11堆牌最上边)
  3. 然后抽出牌堆底的牌,重复第2步
  4. 如果抽到K,就死一条命,从第1步开始
  5. 当四条命都死后(也就是四张K都出现之后)就统计出现了多少组(一组的定义就是出现了四张点数相同的频牌),输出。

思路

1.输入

输入方式如下:

C++
int read()
{
    char c
    cin>>c;
    if(c=='0') return 10;//0
    if(c=='J') return 11;//J
    if(c=='Q') return 12;//Q
    if(c=='K') return 13;//K
    if(c=='A') return 1; //A
    return c-'0';
}

2.模拟

  1. 建一个nex变量表示抽到的是几,ney=nex
  2. 更新nex
  3. cnt(cnticnt_i表示第i组牌抽了几张)数组加1
  4. 弹出队尾的牌
C++
while(nex!=13)//抽到的不能是K
{
  	ll ney=nex;//nex会更新
  	nex=q[nex].back();//抽出队尾的牌
    cnt[ney]++;//cnt+1
  	q[ney].pop_back();//弹出队尾的牌
}
life--;//生命减1

3.输出

C++
int ans=0;
for(int i=1;i<=12;i++)//因为4个K不算
{
    if(cnt[i]==4) ans++;//4张都抽中就算
}
cout<<ans;

整体代码

C++
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N=1e5+2;
deque<ll>q[20];
ll cnt[20];
ll read()
{
	char c;
	cin>>c;
	if(c=='0') return 10;
	if(c=='J') return 11;
	if(c=='Q') return 12;
	if(c=='K') return 13;
	if(c=='A') return 1;
	return c-'0';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie();
	cout.tie();
	for(ll i=1;i<=13;i++)
	{
		for(ll j=1;j<=4;j++) q[i].push_back(read());
	}
	ll lif=4,ans=0,nex;
	while(lif>0)
	{
		nex=q[13].front();
		q[13].pop_front();
		while(nex!=13)
		{
			ll ney=nex;
			nex=q[nex].back();
			cnt[ney]++;
			q[ney].pop_back();
		}
		lif--;
	}
	for(ll i=1;i<=12;i++)
	{
		if(cnt[i]==4) ans++;
	}
	cout<<ans;
	return 0;
}
原文