pat1042Shuffling Machine (20)

本文探讨了洗牌算法的基本概念及其在特定排列洗牌过程中的应用,详细介绍了如何通过循环迭代实现洗牌操作,并输出洗牌后扑克牌的顺序。文章通过实例演示了算法的实现步骤,以及如何根据给定的洗牌次数K,追踪每张扑克牌的位置变化。

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

题意分析:

(1)给出54张扑克牌的原始顺序,给出任意1~54的排列,此排列意味着对本次扑克牌位置的调整,数组中的序号为a的值b,意味着将原来在a位置的扑克牌移动到第b号位置,求按此排列洗牌K次后的顺序。

(2)因为每次所给的调整顺序一样,对于原始序列中的第i个牌,我们暂且就将他记为i,追踪它在K次调整后“花落谁家”,假设为j,然后用另外一个数组newOrder在第j个位置存i.这样每一张扑克牌最终都会有自己的位置,再依次便利这个数组中的元素就知道,这个位置的牌了

可能坑点:

(1)要注意必须用额外的数组来存调整后的顺序,一般注意这点就不会出错,最后的结果输出时,因为起始位置不为0,所以不能用整除和取余来确定前缀和后缀,如:S13,初始位置是13,整除13后为1,取余后为0.

#include <iostream>

using namespace std;

int main()
{
    int K,i=1,j=1;
    int order[55];
    int newOrder[55];
    cin>>K;
    while(i<=54)cin>>order[i++];
    while(j<=54)
    {
        int num=j;
        for(int k=0;k<K;k++)
        {
            int temp=order[num];
            num=temp;
        }
        newOrder[num]=j;
        j++;
    }
    int first=1;
    for(int k=1;k<=54;k++)
    {
        if(first)first=0;
        else cout<<" ";
        if(newOrder[k]>=1&&newOrder[k]<=13)cout<<'S'<<newOrder[k];
        else if(newOrder[k]<=26)cout<<'H'<<newOrder[k]-13;
        else if(newOrder[k]<=39)cout<<'C'<<newOrder[k]-26;
        else if(newOrder[k]<=52)cout<<'D'<<newOrder[k]-39;
        else cout<<'J'<<newOrder[k]-52;
    }
    cout<<endl;
    return 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、付费专栏及课程。

余额充值