PAT甲级1042

1042 Shuffling Machine (20 分)

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, …, S13,
H1, H2, …, H13,
C1, C2, …, C13,
D1, D2, …, D13,
J1, J2

where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.
Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.
Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.
Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

这次还是两种方法,显然较为麻烦的是我的方法,也就是第二个那个长长的代码块,第一个代码块是我仿照柳神的做法写的,简洁明了,首推,为我们解题提供了一种新的思路,这题首先难到我的是英语单词,我是真的没有读懂什么意思。后来翻了一下晴神宝典知道了题目的意思,感觉需要个pat词典了2333.
这题其实很简单,我觉得考点就在两个数组之间的交换,时间复杂度是2*(54+54).
然后我觉得需要学习的就是怎么输出S1S2…显然柳神的方法比我的简单一些,具体怎么简单直接看代码吧

char s[6]={"SHCDJ"};
	for(int i=1;i<=54;i++)
	{
		b[i]=b[i]-1;
		cout<<s[b[i]/13]<<b[i]%13+1;
		if(i!=54)
		cout<<" ";
	}
if(c1[54]==53)
	cout<<"J1";
	else if(c1[54]==54)
	cout<<"J2";
	else if(c1[54]>=1&&c1[54]<=13)
	cout<<"S"<<c1[54];
	else if(c1[54]>=14&&c1[54]<=26)
	cout<<"H"<<c1[54]-13;
	else if(c1[54]>=27&&c1[54]<=39)
	cout<<"C"<<c1[54]-26;
	else if(c1[54]>=40&&c1[54]<=52)
	cout<<"D"<<c1[54]-39;

高下立判了。膜就完了,我相信还有别的简单方法,待开发中ing

#include<iostream>
using namespace std;
int main()
{
	int n;
	int a[55],b[55],c[55];
	cin>>n;
	for(int i=1;i<=54;i++)
		cin>>a[i];
	for(int i=1;i<=54;i++){
		b[i]=i;
	}
	for(int i=1;i<=n;i++)
	{	
		for(int k=1;k<=54;k++)
		c[k]=b[k];
		for(int j=1;j<=54;j++)
		b[a[j]]=c[j];
	}
	char s[6]={"SHCDJ"};
	for(int i=1;i<=54;i++)
	{
		b[i]=b[i]-1;
		cout<<s[b[i]/13]<<b[i]%13+1;
		if(i!=54)
		cout<<" ";
	}
	return 0;
}

啰嗦了一大堆,重点还是看代码吧,这些都是我码的,只是参考了一下柳神的思想哦,大体上是一样的。

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int n;
	int a[55]={0};
	int c1[55]={0},c2[55]={0};
	cin>>n;
	for(int i=1;i<=54;i++)
		cin>>a[i];
	for(int i=1;i<=54;i++){
		c1[i]=i;
		c2[i]=i;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=54;j++)
		c1[a[j]]=c2[j];
		for(int j=1;j<=54;j++)
		c2[j]=c1[j];
	}
	for(int i=1;i<54;i++)
	{
		if(c1[i]==53)
		cout<<"J1"<<" ";
		else if(c1[i]==54)
		cout<<"J2"<<" ";
		else if(c1[i]>=1&&c1[i]<=13)
		cout<<"S"<<c1[i]<<" ";
		else if(c1[i]>=14&&c1[i]<=26)
		cout<<"H"<<c1[i]-13<<" ";
		else if(c1[i]>=27&&c1[i]<=39)
		cout<<"C"<<c1[i]-26<<" ";
		else if(c1[i]>=40&&c1[i]<=52)
		cout<<"D"<<c1[i]-39<<" ";
	}
	if(c1[54]==53)
	cout<<"J1";
	else if(c1[54]==54)
	cout<<"J2";
	else if(c1[54]>=1&&c1[54]<=13)
	cout<<"S"<<c1[54];
	else if(c1[54]>=14&&c1[54]<=26)
	cout<<"H"<<c1[54]-13;
	else if(c1[54]>=27&&c1[54]<=39)
	cout<<"C"<<c1[54]-26;
	else if(c1[54]>=40&&c1[54]<=52)
	cout<<"D"<<c1[54]-39;
}
### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值