PTA自测题---Shuffling Machine(洗牌机C++)

自测-5 Shuffling Machine (20分)

题目描述这里就不描述了

题目链接点我

思路

  1. 摆明了就是交换数组的元素咯,比如说,当前扑克牌顺序是2 3 4 5 1
  2. 而交换数组的顺序是3 2 1 5 4
  3. 这样就是2要换到第三个位置上,3要换到第二个位置上,4换到第一个位置上…这里要注意数组下标从零开始
  4. 于是乎,换完就变成了 4 3 2 1 5
  5. 不过,如果要换两次的话,记住是用第一次换完后的数组再进行交换(再来一次循环)
  6. 还有一个小技巧,就是不用一个char数组存扑克牌的顺序,太麻烦了…我们可以通过顺序判断输出就行了,比如50对应D13,以此类推

AC代码

#include <iostream>
using namespace std;
int dig_2_str(int n)
{
    if(n <= 13)
        return 0;
    else if( n <= 26)
        return 1;
    else if(n <= 39)
        return 2;
    else if(n <= 52)
        return 3;
    else
        return 4;
}
int main()
{
    int n;
    scanf("%d",&n);
    int a[54];
    for(int i=0;i<54;i++)
        cin>>a[i];
    
    int b[n+1][54];
    for(int i=0;i<54;i++)
        b[0][i] = i+1;
        
    int c=0;
    while(n)
    {
        c++;
        for(int i=0;i<54;i++)
        {
            int k = a[i]-1;
            b[c][k] = b[c-1][i];
        }
        n--;
    }
    /* 
    for(int i=0;i<54;i++)
    	printf("%d ",b[2][i]);
    */
    ///*
    int i=0;
    for(;i<53;i++)
        if(dig_2_str(b[c][i]) == 0)
            printf("S%d ",b[c][i]);
        else if(dig_2_str(b[c][i]) == 1)
            printf("H%d ",b[c][i]-13);
        else if(dig_2_str(b[c][i]) == 2)
            printf("C%d ",b[c][i]-26);
        else if(dig_2_str(b[c][i]) == 3)
            printf("D%d ",b[c][i]-39);
        else
            printf("J%d ",b[c][i]-52);
            
    if(dig_2_str(b[c][i]) == 0)
        printf("S%d",b[c][i]);
    else if(dig_2_str(b[c][i]) == 1)
        printf("H%d",b[c][i]-13);
    else if(dig_2_str(b[c][i]) == 2)
        printf("C%d",b[c][i]-26);
    else if(dig_2_str(b[c][i]) == 3)
        printf("D%d",b[c][i]-39);
    else
        printf("J%d",b[c][i]-52);
    return 0;
    //*/
    /*
    for(int i=0;i<54;i++)
    printf("%d ",b[1][i]);
    */
}
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、付费专栏及课程。

余额充值