PAT1 1042 Shuffling Machine

本文介绍了一种基于序列的牌序洗牌算法,通过给定的洗牌次数和序列,实现了对54张扑克牌的重新排序。文章提供了一个Python实现的例子,详细解释了如何读取输入并进行洗牌操作。

题目大意
我的github

题目大意

现在有54张牌,假设一开始的顺序是:

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

现在给一个正整数 K K K表示洗牌的次数,然后再给一个序列,要求将54张牌按照这个序列洗 K K K次。序列中第i个位置是j,就将第i张牌放到第j的位置

输入

每组包含一个测试用例,用例第一行是一个正整数 K ≤ 20 K\leq20 K20
第二行是54个数字表示洗牌的序列

输出

对每个用例,输出洗完牌之后的牌的序列

样例输入

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

解析

按照规则洗 K K K遍牌,然后输出

# -*- coding: utf-8 -*- 
# @Time : 2019/6/4 19:19 
# @Author : ValarMorghulis 
# @File : 1042.py
def solve():
    t = ['S', 'H', 'C', 'D', 'J']
    n = int(input())
    a = list(map(int, input().split()))
    ans = list()
    for i in range(54):
        tmp = "%c%d" % (t[i // 13], i % 13 + 1)
        ans.append(tmp)
    for j in range(n):
        tans = ans.copy()
        for i in range(54):
            tans[a[i]-1] = ans[i]
        ans = tans
    print(*ans)


if __name__ == "__main__":
    solve()

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.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值