原题链接:
思路:
从编号1开始遍历每一张牌的位置变化,即求出这张牌经过K次洗牌后所在的位置。
再将编号转换为每张牌的具体名字,存储到最终结果的数组中。
代码:
#include <iostream>
#include <string>
using namespace std;
#define endl '\n'
int K;
int shuffling[55];
string card[55];
string cvt(int x) {//将数字转换为字符串
string num;
if (x >= 10) {
num += x / 10 + '0';
}
num += x % 10 + '0';
return num;
}
string pre = "SHCDJ";
string i2s(int x) {//将牌的编号转换为具体的牌名
string ans;
ans = pre[(x-1) / 13];
string num = cvt((x-1) % 13+1);
ans += num;
return ans;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> K;
for (int i = 1; i <= 54; i++) {
cin >> shuffling[i];
}
for (int i = 1; i <= 54; i++) {//遍历每一张牌的位置
int position = i;
for (int i = 0; i < K; i++) {
position = shuffling[position];
}
card[position] = i2s(i);
//cout << card[position] << endl;
}
for (int i = 1; i <= 54; i++) {
cout << card[i];
if (i != 54) {
cout << " ";
}
}
return 0;
}