PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

题目描述:

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.

译:洗牌是一种用来随机化一副扑克牌的程序。因为标准的洗牌技术被认为是软弱的,而且为了避免员工通过不充分的洗牌与赌客合作的“内部工作”,许多赌场雇用了自动洗牌机。你的任务是模拟洗牌机。

机器根据给定的随机顺序洗牌54张,并重复给定的次数。假设一张牌组的初始状态按以下顺序排列:

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

“S”代表 黑桃,“H”代表 红心 ,“C”代表 梅花 ,“D”代表 方块,“J”代表 王 。给定的顺序是[1 , 54]中不同整数的排列。如果第 i 个位置的数字是 j ,则意味着将牌从第 i 个位置移动到第 j 个位置。例如,假设我们只有 5 张牌 : S3, H5 , C1 , D13 和 J2。给定洗牌顺序{ 4 , 2 , 5 , 3 , 1 } ,结果为 : J2 , H5 , D13 , S3 , C1。如果我们再次洗牌,结果将是: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.

译:每个输入文件包含一个测试用例,每个用例在第一行中包含一个正整数 K (≤ 20 ) , 表示重复次数。所有的数字被一个空格分隔。


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


The Idea:

首先看最后一题的题目文字数量确实有被下到,但是仔细去理解,其实就是相当于给你一个数组,然后按照数字的初始的每个元素的大小将其放到对应的位置上去,然后重复操作 k 次,得到的位置就是原先牌需要存放的位置。使用 map 来存储数字到牌的映射,使用 string 数组存储最后的洗好牌的数学,采用 int 型数组存储输入的顺序。


The Codes:

#include<bits/stdc++.h>
using namespace std;
int num[55] , n ;
map<int , string > mp ;
string ans[55] ;
void init(){
for(int i = 1 ; i <= 54 ; i ++){
if( i <= 13) mp[i] = ("S" + to_string(i)) ; // 黑桃牌面
else if(i <= 26) mp[i] = ("H" + to_string(i - 13)) ; // 红心牌面
else if(i <= 39) mp[i] = ("C" + to_string(i - 26)) ; // 梅花牌面
else if (i <=52) mp[i] = ("D" + to_string(i - 39 )); // 方块牌面
else if(i == 53) mp[i] = "J1" ; // 小王
else mp[i] = "J2" ; // 大王
}
}
int main(){
init() ; // 完成数字到牌面的初始化映射
scanf("%d" , &n) ;
for(int i = 1 ; i <= 54 ; i ++) scanf("%d" ,&num[i]) ; // 存储输入的顺序
for(int i = 1 ; i <= 54 ; i ++){
int temp = num[i] ; // 获得当前数字
for(int j = 0 ; j < n ; j ++) temp = num[temp] ; // temp 中存储第 j 次洗牌后的位置
ans[temp] = mp[num[i]] ; // 在该位置上存储 原先数字对应的 牌面
}
// 输出牌面 , 采用 printf 输出字符串时 需要用 c_str() 进行转换
for(int i = 1 ; i <= 54 ; i ++) printf("%s%c",ans[i].c_str() , (i==54)?'\n':' ');
return 0;
}

PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642的更多相关文章

  1. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  2. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  3. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  4. PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  5. PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  6. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  8. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  9. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

随机推荐

  1. 看超额担保免信任的NGK DeFi 乐高如何打造下一个千倍币?

    2020年中,DeFi的高收益率吸引了大量热钱涌入,DeFi总锁仓量破百亿美金.如今,流动性挖矿的热潮暂时停歇,但对于 NGK DeFi项目来说,它背后的演变进化从未停止. 免信任是 NGK DeFi ...

  2. 「NGK每日快讯」12.24日NGK第51期官方快讯!

  3. 关于Python 编码的一点认识

    在计算机中,所有数据的存储.运算以及传输都必须是二进制数字,因为计算机只认识0和1. 当一个人把一份数据传给另一个人时,计算机传递的是其实是二进制数字,但这些数字需要被还原为原始信息. 这个工作当然是 ...

  4. Python爬虫_百度贴吧(title、url、image_url)

    本爬虫以百度贴吧为例,爬取某个贴吧的[所有发言]以及对应发言详情中的[图片链接] 涉及: request 发送请求获取响应 html 取消注释 通过xpath提取数据 数据保存 思路: 由于各贴吧发言 ...

  5. React组件复用的方式

    React组件复用的方式 现前端的工程化越发重要,虽然使用Ctrl+C与Ctrl+V同样能够完成需求,但是一旦面临修改那就是一项庞大的任务,于是减少代码的拷贝,增加封装复用能力,实现可维护.可复用的代 ...

  6. Mybites逆向工程的搭建

    这个链接写的很全:https://www.cnblogs.com/whgk/p/7140638.html 这段时间太忙,等周末写上自己尝试的步骤.暂时仅作记录.

  7. oracle can't kill session

    oracle 在杀会话时,会出现杀不掉的情况. 原因是在回滚大事物   解决方法: alter system disconnect session 'sid, serial#' immediate; ...

  8. js中数据、内存、变量的概念及三者之间的关系

    目录 数据.内存.变量的概念及三者之间的关系 什么是数据 数据的特点 什么是内存 栈内存 堆内存 JS引擎如何管理内存 什么是变量 变量是普通类型时 变量是引用类型时 数据.内存.变量的三者之间的关系 ...

  9. CSS元素层级的概念及性质

    元素的层级的介绍 什么是元素的层级 通过z-index可以改变开启定位元素的层级 父元素的层级再高也不会遮盖住子元素 元素的层级的介绍 什么是元素的层级 当元素开启定位后就会是元素提升一个层级,网页是 ...

  10. 免费报表工具 积木报表(JiMuReport)的安装

    分享一b/s报表工具(服务),积木报表(JiMuReport),张代浩大佬出品. 官网:http://www.jimureport.com/ 离线版官方下载:https://github.com/zh ...