00-自测5. Shuffling Machine (20)

00-自测5. Shuffling Machine (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

提交代码

APL上的题目好多人都做过,比ACM要简单一些,但是也有难度。这个题目我看了将近半个小时才看懂,英语是短板,有时间我一定要恶补一下。题目的意思是设计一个程序,模仿洗牌机洗牌,给定每张牌的位置替换序列,算出洗牌后的排列顺序。比如原有的次序为S3,H5,C1,D13,J2,给定的位置替换顺序为4,2,5,3,1,那么一次洗牌的结果为C1,H5,S3,J2,D13,需要两次洗牌的结果为C1,H5,S3,J2,D13。其中S代表黑桃,H代表红心,C代表梅花,D代表方块,J代表王。

#include<iostream>  
  
void OutputWord(int a)  
{  
    int flag = a/13;  
    if(0 == flag)  
        printf("S");  
    else if(1 == flag)  
        printf("H");  
    else if(2 == flag)  
        printf("C");  
    else if(3 == flag)  
        printf("D");  
    else if(4 == flag)  
        printf("J");  
  
    printf("%d", a%13+1);  
}  
#define NUM 54  
int main()  
{  
    int n;  
    int s[NUM];  
    int a[NUM];  
    while( scanf("%d", &n) != EOF )  
    {  
        for(int i = 0; i < NUM; ++i)  
        {  
            a[i] = i;  
            scanf("%d", &s[i]);  
        }  
        //shuffling  
        while(n--)  
        {  
            int a_back[NUM];  
            for(int i = 0; i < NUM; ++i)  
                a_back[i] = a[i];  
            for(int i = 0; i < NUM; ++i)  
                a[s[i]-1] = a_back[i];   
        }  
        //output  
        for(int i = 0; i < NUM; ++i)  
        {  
            OutputWord(a[i]);  
            //printf("%d", a[i]);  
            if(i != NUM-1)  
                printf(" ");  
            else  
                printf("\n");  
        }  
    }  
    return 0;  
}  



这里还有其他解法:

1.http://blog.csdn.net/xu3737284/article/details/43112401

2.http://blog.csdn.net/andrewseu/article/details/44194755

3.http://blog.csdn.net/fang_abc/article/details/44114043

4.http://www.cnblogs.com/Zengineer/p/4315803.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。