UVA10205 - Stack ‘em Up(模拟)
题目大意:给你52张牌,这些牌一开始就有个顺序。现在给你每次的洗牌动作,52个数Ai,表示第i个位置上的牌放到Ai位置。意思就是能够通过这次洗牌,可以将i位置上的牌放到Ai位置上。至于后面的牌要不要移动什么的,根本不考虑。反正就是通过这次的洗牌,我给你52个数,把每个位置上的牌更新了一下。
解题思路:之前的题意看错,还以为是每次洗牌然后插入,后面才发现题意是这样的。那么只需要记录下上次洗牌的每个位置的牌,然后在将这次洗牌动作后的每个位置的牌更新,模拟洗牌,最后输出每个位置的牌。
代码:
#include <cstdio>
#include <vector>
using namespace std;
const char Suit[4][10] = {"Clubs", "Diamonds", "Hearts", "Spades"};
const char Value[13][10] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
const int maxn = 105;
int op[maxn][52];
int d[52];
struct card {
int suit;
int value;
card(int suit = 0, int value = 0) {
this->suit = suit;
this->value = value;
}
};
vector<card> v;
void init () {
for (int i = 0; i < 52; i++)
d[i] = i;
}
void Operator (int K) {
int c[52];
for (int i = 0; i < 52; i++)
c[i] = d[op[K][i] - 1];
for (int i = 0; i < 52; i++)
d[i] = c[i];
}
void output() {
for (int i = 0; i < 52; i++)
printf ("%s of %s\n", Value[v[d[i]].value], Suit[v[d[i]].suit]);
}
int main () {
int T, N, K;
char str[maxn];
scanf ("%d", &T);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 13; j++)
v.push_back(card(i, j));
while (T--) {
scanf ("%d", &N);
init();
for (int i = 0; i < N; i++)
for (int j = 0; j < 52; j++)
scanf ("%d", &op[i][j]);
getchar();
while (gets(str) != NULL && str[0] != ‘\0‘) {
sscanf (str, "%d", &K);
Operator(K - 1);
}
output();
if (T)
printf ("\n");
}
return 0;
}
UVA10205 - Stack 'em Up(模拟)
时间: 2024-10-13 01:02:55