首页 > 代码库 > PAT甲题题解-1042. Shuffling Machine (20)-模拟

PAT甲题题解-1042. Shuffling Machine (20)-模拟

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6789205.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~

 

给出洗牌次数,以及洗牌的序列规则
第i个数shuffles[i]表示要将第i张牌移到第shuffles[i]个

很简单,就是
shuffle_seq[shuffles[i]]=start_seq[i];

技术分享
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
using namespace std;
int start_seq[55]; //洗牌前的序列
int shuffle_seq[55]; //洗牌后的序列
int shuffles[55]; //洗牌规则
string card[55];
int main()
{
    int k;
    int n=54;
    char str[5];
    string s;
    scanf("%d",&k);
    for(int i=1;i<=13;i++){
        //itoa(i,str,10);//会导致编译错误,在部分编译器上没法使用
        sprintf(str,"%d",i);
        s=str;
        card[i]="S"+s;
    }
    for(int i=14;i<=26;i++){
        sprintf(str,"%d",i-13);
        s=str;
        card[i]="H"+s;
    }
    for(int i=27;i<=39;i++){
        sprintf(str,"%d",i-26);
        s=str;
        card[i]="C"+s;
    }
    for(int i=40;i<=52;i++){
        sprintf(str,"%d",i-39);
        s=str;
        card[i]="D"+s;
    }
    card[53]="J1";
    card[54]="J2";

    for(int i=1;i<=n;i++){
        scanf("%d",&shuffles[i]);
        start_seq[i]=i;
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<=n;j++){
            shuffle_seq[shuffles[j]]=start_seq[j];
        }
        for(int j=1;j<=n;j++){
            start_seq[j]=shuffle_seq[j];
        }
    }
    cout<<card[start_seq[1]];
    for(int i=2;i<=n;i++){
        cout<<" "<<card[start_seq[i]];
    }
    printf("\n");
    return 0;
}
View Code

 

PAT甲题题解-1042. Shuffling Machine (20)-模拟