PAT 1042 Shuffling Machine

#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

char tbl[5] = {‘S‘, ‘H‘, ‘C‘, ‘D‘, ‘J‘};

void shuffle(vector<char> &card, vector<char> &rnd) {
    int rlen= rnd.size();
    vector<char> tmp(card.size(), 0);
    for (int i=0; i<rlen; i++) {
        tmp[rnd[i]] = card[i];
    }
    card = tmp;
}

void print_card(char card) {
    printf("%c%d", tbl[card/13], card % 13 + 1);
} 

int main() {
    int times = 0, r = 0;
    vector<char> rnd(54, 0);
    vector<char> card(54, 0);
    for (int i=0; i<54; i++) {
        card[i] = i;
    }
    scanf("%d", &times);
    for (int i=0; i<54; i++) {
        scanf("%d", &r);
        rnd[i] = r - 1;
    }
    for (int i=0; i<times; i++) {
        shuffle(card, rnd);
    }
    print_card(card[0]);
    for (int i=1; i<54; i++) {
        printf(" ");
        print_card(card[i]);
    }
    return 0;
}

一开始想复杂了以为要像算法导论里面提到的那样进行元素交换,那里是因为随机数列不能保证在一定范围内唯一(如果要得到这样的数列其实也可以,就相当于已经进行了一次洗牌)所以遍历数组时产生的随机数是多少就把当前元素和下标和当前随机数一致的元素对换。不过这里已经说了数列是不重复的就直接把元素放到对应的位置上即可。当然程序里可以不把单次shuffle写成一个函数,这样可以避免反复创建vector,只要使用一个临时vector即可,多执行几次swap来代替元素拷贝。

时间: 2024-10-24 23:28:38

PAT 1042 Shuffling Machine的相关文章

PAT 1042. Shuffling Machine (20)

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

pat 1042 Shuffling Machine(20 分)

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

1042. Shuffling Machine

1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 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

1042 Shuffling Machine (20 分)

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

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 cas

PAT A1042. 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 cas

1042. Shuffling Machine (20) - sstream实现数字转字符串

题目如下: 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, ma

PAT(A) 1042. Shuffling Machine (20)

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, D

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