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:
1 | S1, S2, ..., S13, |
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:
1 | 2 |
Sample Output:
1 | 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 |
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
题目大意
一个赌场使用机器洗牌,给出洗的轮次n以及顺序,求洗完的结果。如给出4, 2, 5, 3, 1,则把第1张牌放到第4个位置,第2张牌放到第2个位置,第3张牌放到第5个位置,依次类推。
分析
用vector<string> cards
记录牌,vector<int> order
记录第i个位置上放第几张牌,vector<int> shuf
记录洗牌规则。循环卡牌堆,利用x暂存i,再令x=shuf[x],循环n次,结束后第x位置上即为第i张牌。以题中例子来说:
原始牌 | S3 | H5 | C1 | D13 | J2 |
洗一轮 | J2 | H5 | D13 | J2 | C1 |
洗二轮 | C1 | H5 | S3 | J2 | D13 |
shuf | 4 | 2 | 5 | 3 | 1 |
index | 1 | 2 | 3 | 4 | 5 |
看第二轮结果,shuf[1]=4,shuf[4]=3,第一张牌为cards[3]=C1。第二张牌始终为2。shuf[3]=5,shuf[5]=1,第三张牌为cards[1]=S3,依次类推。
代码
1 |
|