[PAT A1042]Shuffling Machine

[PAT A1042]Shuffling Machine

题目描述

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.

输入格式

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.

输出格式

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.

输入样例

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

输出样例

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

题目解读(练解题的同学先不要看哦~)

  1. 题目的意思是先是54张牌按照S1-S13(黑桃1-13),H1-H13(红桃1-13),C1-C13(梅花1-13),D1-D13(方片1-13),J1-J2(小大王)排列,然后输入K,表示按照接下来的方法洗牌K次
  2. 洗牌的方法就是输入54个数字,第i个数字F[i]表示将第I张牌放到第F[i]个位置上
  3. 最后格式就是输出经过K次洗牌后的排序
  4. 以五张牌为例,之前的牌序是S1,S2,S3,S4,S5,那么给定{4,2,5,3,1}表示
    将第1个位置上的牌放到第4个位置上,
    将第2个位置上的牌放到第2个位置上,
    将第3个位置上的牌放到第5个位置上,
    将第4个位置上的牌放到第3个位置上,
    将第5个位置上的牌放到第1个位置上,
    结果是{S5,S2,S4,S1,S3}

解析

  1. 我首先使用了xs(相当于x+start表示第一个出现x的位置)来确保他们之间的关系,例如我使用1-54编号S1-S13,H1-H13,C1-C13,D1-D13,J1-J2,那么第一个出现s的位置就是1,第一个出现h的位置就是14,依次类推,那么就把1-54编号和每张牌之间形成了一个函数关系,通过这个函数关系我们可以输出最终的结果
  2. 记住输出最后一张牌的时候不能加“ ”(空格),否则会导致输出格式的错误!
#include<iostream>
#include<string>
using namespace std;
int before[55], after[55], F[55]; //before存放的是洗牌之前的排序,after存放洗牌后的排序
//f表示function,是存放题目给定的映射关系的函数
int main()
{
 int K;
 int ss = 1, hs = 14, cs = 27, ds = 40, js = 53;//这里我用xs(相当于x+start表示第一个出现x的位置)
 //我使用1-54编号S1-S13,H1-H13,C1-C13,D1-D13,J1-J2,那么第一个出现s的位置就是1,第一个出现h的位置就是14,依次类推
 cin >> K;
 for (int i = 1; i <= 54; i++) before[i] = i;  //未排序时的排序,初始化
 for (int i = 1; i <= 54; i++) cin >> F[i];    //映射关系
 while (K--)//一共要洗K次
 {
  for (int i = 1; i <= 54; i++) after[F[i]] = before[i]; 
  //理解为:未洗牌是第i个位置上的牌(before[i])放到洗牌后的第F[i]个位置上
  for (int i = 1; i <= 54; i++) before[i] = after[i];
  //把洗过的牌重新放回before数组,做好下一次洗牌的准备
 }
 for (int i = 1; i <= 54; i++) //用于输出,表明了对应关系
 {
  if (ss <= before[i] && before[i]<hs) cout << "S" << before[i] - ss + 1;
  else if (before[i] < cs) cout << "H" << before[i] - hs + 1;
  else if (before[i] < ds) cout << "C" << before[i] - cs + 1;
  else if (before[i] < js) cout << "D" << before[i] - ds + 1;
  else cout << "J" << before[i] - js + 1;
  if (i != 54) cout << " ";
 }
 return 0;
}

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出自己宝贵的意见!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A shuffling machine in C++ can be implemented using an array to represent the deck of cards and using the random number generator to shuffle the cards. Here is a sample code for a shuffling machine: ``` #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int NUM_CARDS = 52; class ShufflingMachine { private: int deck[NUM_CARDS]; int position; public: ShufflingMachine() { for (int i = 0; i < NUM_CARDS; i++) { deck[i] = i; } position = 0; } void shuffle() { srand(time(NULL)); for (int i = 0; i < NUM_CARDS; i++) { int j = rand() % NUM_CARDS; swap(deck[i], deck[j]); } position = 0; } int dealCard() { if (position >= NUM_CARDS) { shuffle(); } return deck[position++]; } }; int main() { ShufflingMachine shuffler; shuffler.shuffle(); for (int i = 0; i < NUM_CARDS; i++) { cout << shuffler.dealCard() << " "; } cout << endl; return 0; } ``` In this code, the `ShufflingMachine` class represents the shuffling machine. The `deck` array stores the deck of cards, and the `position` variable keeps track of the current position in the deck. The `shuffle` method shuffles the deck by randomly swapping cards. It uses the `srand` function to seed the random number generator with the current time, and the `rand` function to generate random indices for swapping cards. The `dealCard` method deals the next card from the deck. If the deck has been exhausted, it calls the `shuffle` method to shuffle the cards again. In the `main` function, we create a `ShufflingMachine` object and shuffle the cards. Then we deal all the cards and print them out.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值