Shuffling Machine(C语言实现)

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.

解题思路:
刚开始看到题目就感到很害怕,全英文的题目,不过仔细看一看题目中所举的例子就明白题目的含义了。接下来就是解决问题啦,按照IOP方法,先输入数据,之后处理数据,接着输出数据。

输入数据:包含两个内容,一个是参数k,也就是要执行的循环次数;另一个是给定的顺序,可以用order数组来存储,数组的长度是54。

处理数据:首先要考虑的是如何存储54张纸牌,我的想法是用两个数组,一个整型数组存储纸牌上的数字(1,2,……,13),另一个是字符数组存储纸牌上的字母(S, H, C,D,J )。之后在使用两个数组来存储按照指定顺序变化后的纸牌内容。接下来进行循环。

输出数据:注意空格的问题即可。

实现代码
#include 
int main(){
    int i, j, k, order[54] = {0};
    int arr[54] = {0}, brr[54] = {0};
    char crr[54], drr[54];
    
    scanf("%d", &k); //输入数据
    for (i=0; i<54; i++){
        scanf("%d", &order[i]);
    }
    
    //处理数据
    for (i=0; i<54; i++){ //存储纸牌信息
        arr[i] = i % 13 + 1; //arr数组存放纸牌上的数字
        if (i < 13){
            crr[i] = 'S'; //crr数组存放纸牌上的字母
        }
        else if (i < 26){
            crr[i] = 'H';
        }
        else if (i < 39){
            crr[i] = 'C';
        }
        else if (i < 52){
            crr[i] = 'D';
        }
        else{
            crr[i] = 'J';
        }
    }
    for (i=0; i<k; i++){ //执行循环
        for (j=0; j<54; j++){
            brr[order[j] - 1] = arr[j]; //brr数组存放变化后的纸牌上的数字
            drr[order[j] - 1] = crr[j]; //drr数组存放变化后的纸牌上的字母
        }
        for (j=0; j<54; j++){ //将变化后的纸牌内容写回到原数组再进行循环
            arr[j] = brr[j];
            crr[j] = drr[j];
        }
    }
    
    for (i=0; i<54; i++){ //输出数据
        if (i == 0){
            putchar(crr[i]);
        }
        else{
            printf(" %c", crr[i]);
        }
        printf("%d", arr[i]);
    }
    
    return 0;
}

你可能感兴趣的:(c语言)