一、题目描述

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

二、题目重述

给定初试序列 S1,S2...S13, H1,H2...H13, C1,C2...C13, D1,D2...D13, J1,J2,共52个字符串,它们分别代表54张牌。显然这个算法是一个特殊的洗牌方法,为了防止有人出老千,是这样的一个背景,所以就有了本题的洗牌题。接下来对这个序列进行这个算法,比如有5张牌S3,H5,C1,D13,J2,然后输入序列{4,2,5,3,1}就会把S3放到4号位置,把H5放到2号位置,C1放到5号位置,D13放到3号位置,J2放到1号位置,于是就得到了{J2,H5,D13,S3,C1}序列,如果再进行一次这样的操作,其实就是在上面变换之后序列的基础上再进行一次这样的变化,{J2,H5,D13,S3,C1}就变成了{C1,H5,S3,J2,D13}。

三、算法思路

3.1、要存储的数据

  1. 对输入的数据进行保存:重复操作的次数n,以及54位变换序列(也就是洗牌顺序)
  2. 变换前的牌组顺序
  3. 变换后的牌组顺序

因此总结来看:

int n;
int before[54],after[54],change[54];

四、参考代码

#include <iostream>
using namespace std; int main()
{
int before[54];
//0~12 - S1,S2,...S13
//13~25 - H1,H2,...H13
//26~38 - C1,C2,...C13
//39~51 - D1,D2,...D13
//52,53 -J1,J2
//before[i]=index of array
for(int i=0;i<54;i++){
before[i]=i;
}
int after[54];
int change[54];
//n
int n;
cin>>n; for(int i=0;i<54;i++){
cin>>change[i];
change[i]--;//change-1 to after's index
}
//exchange n times
while(n--){
for(int i=0;i<54;i++){
int temp=change[i];
after[temp]=before[i];
}
for(int i=0;i<54;i++){
before[i]=after[i];
}
}
for(int i=0;i<54;i++){
if(after[i]>=0 && after[i]<=12){
//S1--S13
if(i<53){
cout<<'S'<<after[i]+1<<' ';
}
else{
cout<<'S'<<after[i]+1;
}
}
if(after[i]>=13 && after[i]<=25){
//H1--H13
if(i<53){
cout<<'H'<<after[i]-12<<' ';
}
else{
cout<<'H'<<after[i]-12;
}
}
if(after[i]>=26 && after[i]<=38){
//C1--C13
if(i<53){
cout<<'C'<<after[i]-25<<' ';
}
else{
cout<<'C'<<after[i]-25;
}
}
if(after[i]>=39 && after[i]<=51){
//D1--D13
if(i<53){
cout<<'D'<<after[i]-38<<' ';
}
else{
cout<<'D'<<after[i]-38;
}
}
if(after[i]==52){
if(i<53){
cout<<"J1 ";
}
else{
cout<<"J1";
}
}
if(after[i]==53){
if(i<53){
cout<<"J2 ";
}
else{
cout<<"J2";
}
}
}
}

在一些地方可以进行优化,

  1. 比如序列与花色的对应,可以用hash的思想进行映射,不必给每种情况一个if,因为他们是有规律的。
  2. 再比如每个数组下标从1开始存放数据,在计算对应的时候更加简便。
#include <iostream>
using namespace std;
const int N=54;
int before[N+1],after[N+1],change[N+1];
/*
1~13--S1~S13
14~26--H1~H13
27~39--C1~C13
40~52--D1~D13
53~54--J1~J2
*/
char mp[5]={'S','H','C','D','J'};
int main()
{
int n;
cin>>n;
for(int i=1;i<=N;i++){
before[i]=i;//1-55 stand for S1,S2....
}
for(int i=1;i<=N;i++){
cin>>change[i];
}
while(n--){
for(int i=1;i<=N;i++){
after[change[i]]=before[i];
}
for(int i=1;i<=N;i++){// after to before
before[i]=after[i];
}
}
//output after[N+1] from 1-54
for(int i=1;i<=N;i++){
if(i!=1)cout<<' ';
cout<<mp[(after[i]-1)/13]<<(after[i]-1)%13+1;
/*
after[i]=27,27-1=26,26/13=2,mp[2]=C,26%13+1=1,27--C1,correct
*/
}
}

1042 Shuffling Machine的更多相关文章

  1. PAT 1042. Shuffling Machine (20)

    1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...

  2. PAT 1042 Shuffling Machine[难]

    1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...

  3. 1042 Shuffling Machine (20 分)

    1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. Be ...

  4. pat 1042 Shuffling Machine(20 分)

    1042 Shuffling Machine(20 分) Shuffling is a procedure used to randomize a deck of playing cards. Bec ...

  5. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

  6. PAT 1042 Shuffling Machine (20 分)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

  7. PAT甲级——1042 Shuffling Machine

    1042 Shuffling Machine Shuffling is a procedure used to randomize a deck of playing cards. Because s ...

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

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  9. 1042. Shuffling Machine (20) - sstream实现数字转字符串

    题目例如以下: Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffli ...

  10. 1042. Shuffling Machine (20)

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

随机推荐

  1. P3565 [POI2014]HOT-Hotels

    题目描述 There are nn towns in Byteotia, connected with only n-1n−1 roads. Each road directly links two ...

  2. MFC程序消息处理的顺序

    MFC应用程序中处理消息的顺序 1.AfxWndProc()      该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc 2.AfxCallWndProc() 该函 ...

  3. c++——引用

    1 引用概念 a)         在C++中新增加了引用的概念 b)         引用可以看作一个已定义变量的别名 c)         引用的语法:Type& name = var; ...

  4. 在存放源程序的文件夹中建立一个子文件夹 myPackage。例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage)。在 myPackage 包中创建一个YMD类,该类具有计算今年的年份、可以输出一个带有年月日的字符串的功能。设计程序SY31.java,给定某人姓名和出生日期,计算该人年龄,并输出该人姓名、年龄、出生日期。程序使用YM

    题目补充: 在存放源程序的文件夹中建立一个子文件夹 myPackage.例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage).在 m ...

  5. html中radio、checkbox选中状态研究

    我们在web页面开发中经常需要让单选框.复选框进行选中或者不选中的操作, 我们可以在元素中添加checked属性 或者添加checked="checked" 都可以让某个选项默认选 ...

  6. NYOJ17 最长单调递增子序列 线性dp

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=17 分析: i=1 dp[i]=1 i!=1 dp[i]=max(dp[j]+1) ...

  7. 如何在Windows版本的VMware虚拟机上安装苹果系统

    有时我想玩玩苹果系统,但自己有没有mac,只能在虚拟机上装一个苹果玩玩,但又由于某些原因虚拟机软件VMware不支持安装苹果系统,还在有大佬出于不明目的,在网上散布了适用于Windows版本的VMwa ...

  8. Yii2 查询条件

    Model::find() 字符串格式,例如:'status=1' 哈希格式,例如: ['status' => 1, 'type' => 2] 操作符格式,例如:['like', 'nam ...

  9. docker的简单使用----适用于新手

    Docker 1.简介 Docker是一个开源的应用容器引擎 将软件编译成一个镜像:然后在镜像里各种软件做好配置,将镜像发布出去,其他的使用这就可以直接使用这个镜像.运行中的这个镜像叫做容器,容器启动 ...

  10. 深度学习框架caffe在ubuntu下的环境搭建

    深度学习实验室服务器系统配置手册 目录:     一,显卡安装     二,U盘启动盘制作     三,系统安装     四,系统的基本配置     五,安装Nvidia驱动     六,安装cuda ...