PAT 1042 Shuffling Machine (20 分)Java实现

本文详细解析了一种洗牌算法的实现过程,通过模拟下标变换而非直接移动元素,使用两个数组记录移动前后的次序,实现了给定顺序的洗牌操作。文章提供了完整的Java代码示例,展示了如何对一副扑克牌进行多次洗牌。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Shuffling Machine

1. 题意

题目意思就是按照给定的顺序洗牌
比如说题目所给的五张牌样例S3, H5, C1, D13, J2
洗牌顺序{4, 2, 5, 3, 1}
所以我们将

  • S3移动到第四位
  • H5移动到第二位
  • C1移动到第五位
  • D13移动到第三位
  • J2移动到第一位
    得到的新次序是J2, H5, D13, S3, C1
    然后再洗一次得到的次数就是C1, H5, S3, J2, D13

2. 思路

我们不能直接的移动元素来进行变换, 我们得模拟下标的变换
使用两个数组记录移动前的次序和移动后的次序

3. 代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
    static final int n = 54;

    public static void main(String[] args) throws IOException {
        char[] ch = { 'S', 'H', 'C', 'D' };
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                StringBuilder bd = new StringBuilder();
                bd.append(ch[i]);
                bd.append(j);
                list.add(bd.toString());
            }
            if (i == 3) {
                list.add("J1");
                list.add("J2");
            }
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int k = Integer.parseInt(br.readLine());
        String[] s = br.readLine().split(" ");
        int[] num = new int[60];
        for (int i = 0; i < n; i++) {
            num[i] = Integer.parseInt(s[i]);
        }
        int[] start = new int[55];
        int[] end = new int[55];
        for (int i = 0; i < n; i++) {
            start[i] = i;
        }

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < n; j++) {
                end[num[j] - 1] = start[j];
            }
            for (int j = 0; j < n; j++) {
                start[j] = end[j];
            }
        }

        for (int i = 0; i < n; i++) {
            if (i != 0) {
                System.out.print(" ");
            }
            System.out.print(list.get(start[i]));

        }
        System.out.println();
    }
}

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、付费专栏及课程。

余额充值