7-39 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:

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

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:

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

Sample Output:

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

题目第一眼看到的时候我也是懵的没想过还有英文的题,甩给谷歌翻译以后大致明白了题目要求。

题目大致的意思是有一副扑克牌S,H,C,D,J分别代表黑桃,红桃,梅花,方片和王牌。然后输入数据是一个整数n代表洗牌次数第二个是一组数据代表洗牌的顺序(其实就是按照这个顺序进行洗牌),问题在于不管洗多少次都要按照规定的顺序,所以必须装进一个数组里多次使用。

我的思路是,通过给每张牌添加一个排名在对着排名进行快排这样就非常方便完成了他要求的洗牌动作。

具体代码如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct puke
{
    char paim[3];//牌色
    int tap;//排名
} pk;
int cmp(void const *a, void const *b)//qsort 必要的排序函数(形参写死的不能更改,语法就是这样,函数名无所谓)
{
    return ((pk *)a)->tap - ((pk *)b)->tap;//这里因为形参接受成了void型所以需要强转
}
int main()
{
    char temp;
    //初始化一下扑克牌
    pk cards[54] = {
        "S1", 1, "S2", 2, "S3", 3, "S4", 4, "S5", 5, "S6", 6, "S7", 7, "S8", 8, "S9", 9, "S10", 10, "S11", 11, "S12", 12, "S13", 13,
        "H1", 14, "H2", 15, "H3", 16, "H4", 17, "H5", 18, "H6", 19, "H7", 20, "H8", 21, "H9", 22, "H10", 23, "H11", 24, "H12", 25, "H13", 26,
        "C1", 27, "C2", 28, "C3", 29, "C4", 30, "C5", 31, "C6", 32, "C7", 33, "C8", 34, "C9", 35, "C10", 36, "C11", 37, "C12", 38, "C13", 39,
        "D1", 40, "D2", 41, "D3", 42, "D4", 43, "D5", 44, "D6", 45, "D7", 46, "D8", 47, "D9", 48, "D10", 49, "D11", 50, "D12", 51, "D13", 52,
        "J1", 53, "J2", 54};
    int n, x, i, j;
    int tap[54];//接受洗牌顺序的列表
    scanf("%d", &n);
    for (j = 0; j < 54; j++)
    {
        scanf("%d", &x);
        tap[j] = x;
    }
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 54; j++)
            cards[j].tap = tap[j];//为扑克牌添加新的顺序
        qsort(cards, 54, sizeof(pk), cmp);//调用快排
    }
    for (i = 0; i < 54; i++)//最后输出就行
    {
        if (i)
            printf(" ");
        printf("%s", cards[i].paim);
    }
    return 0;
}

个人一点拙见,如有雷同不胜荣幸

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值