1042 Shuffling Machine (20分)

@[TOC](1042 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.
Shuffling:洗牌;
a deck of:一幅;
weak:有缺陷的;
“inside jobs”:内部操作;
gambler:赌徒;
casino:赌场;

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.
Spade:黑桃;
Club:梅花;
Diamond:方块;
Joker:小丑,皇牌,大王和小王;
permutation:排列;

思路

步骤1: 设置两个数组start[]end[],分别用来存放执行操作前后的牌序。在每次操作后,用数组end[]覆盖数组start[],进而方便下一次操作使用。
步骤2: 输出需要用花色表示,则可以使用char型数组mp[] = {S,H,C,D,J}来建立编号与花色之间的关系。假设当前牌号为x,则这张牌对应花色即为mp[(x-1)/13],且所在花色下的编号为(x-1)%13+1
注意点:
1、编号与花色之间的对应关系,特别注意牌的编号减一的原因;
2、注意输出格式的控制,不允许在一行的末尾处多出空格,否则会返回“格式错误”;
3、next 和 end 不可以用于命名,需要替换。因为 next 在 C++ 中属于保留字,而 end() 是 C++ 中的一个函数。

参考代码

// An highlighted block
#include<iostream>
using namespace std;
const int N = 54;
char mp[5]={'S','H','C','D','J'};
int start[N+1],End[N+1],Next[N+1];
int main()
{
    //输入
    int K;
    scanf("%d",&K);
    for(int i=1;i<=N;i++)start[i]=i;//初始化牌的编号
    for(int i=1;i<=N;i++){
        scanf("%d",&Next[i]);
    }
    //运算
    //for(int step=0;step<K;step++){
    while(K--){
        for(int i=1;i<=N;i++){
            End[Next[i]]=start[i];//把第i个位置的牌的编号存于位置next[i]
        }
        for(int i=1;i<=N;i++){
            start[i]=End[i];
        }
    }
    //输出
    for(int i=1;i<=N;i++){
        if(i!=1)printf(" ");//控制输出格式
        printf("%c%d",mp[(start[i]-1)/13],(start[i]-1)%13+1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值