题目
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.
输入
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.
输出
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.
思路
把54张扑克牌存进数组,对于每一次打乱顺序,新的数组用来盛放打乱顺序后的扑克牌
最终代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
string card[54];
string newcard[54];
int order[54];
string acard(char c, int i) {
string s;
s += c;
if (i < 10) s += i + '0';
else {
s += i / 10 + '0';
s += i % 10 + '0';
}
return s;
}
void init() {
for (int i = 0; i < 13; i++) {
card[i] = acard('S', i + 1);
card[i + 13] = acard('H', i + 1);
card[i + 13 * 2] = acard('C', i + 1);
card[i + 13 * 3] = acard('D', i + 1);
}
card[52] = "J1"; card[53] = "J2";
}
int main() {
init();
int N;
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < 54; j++) {
if(i==0)
cin >> order[j];
newcard[order[j] - 1] = card[j];
}
for (int j = 0; j < 54; j++)
card[j] = newcard[j];
}
for (int i = 0; i < 53; i++)
cout << card[i] << ' ';
cout << card[53] << endl;
return 0;
}
总结
比较简单