题目大意:
有54张牌,编号为1~ 54,初始按编号从小到大排列。另外,这些牌按初始排列给定花色,即从左至右分别为13张S、13张H、13张C、13张D、2张J,如下所示:
S1, S2.., S13, H1, H2,.., H13, C1, C2,..,CI3, D1, D2,., D13,J1,J2
接下来执行一种操作, 这种操作将牌的位置改变为指定位置。例如有5张牌S3, H5, C1,D13, J2,然后给定操作序列{4,2, 5,3,1},因此把S3放到4号位、把H5放到2号位、C1放到5号位、D13 放到3号位、J2放到1号位,于是就变成了J2, H5, D13, S3, C1。现在需要将这种操作执行K次,求最后的排列结果。例如上面的例子中,如果执行第二次操作,那么序列J2, H5, D13, S3,C1就会变成C1, HS, S3, J2, D13.
算法思路:
题目要求模拟洗牌过程,给定初始牌组为cards,起始下标位置从1开始,给定洗牌顺序orders,和洗牌次数K,要求得出最终洗牌结果。题目比较简单,就是使用hash的思想,我们每次洗牌就创建一个新的数组new_cards暂存最终洗牌结果,该数组的值取决于orders与cards,其对应关系为new_cards[orders[j]] = cards[j];在new_cards数组求解完毕后,将new_cards数组赋值给cards即可,每次循环执行该过程,直到结束
提交结果:
AC代码:
#include<cstdio>
#include<string>
using namespace std;
string cards[55]={"","S1", "S2", "S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13",
"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13",
"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13",
"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","J1","J2"};
int main(){
int K;//重复次数
scanf("%d",&K);
int orders[55];
for(int j=1;j<55;++j){
scanf("%d",&orders[j]);
}
for(int i=0;i<K;++i){
string new_cards[55];
for(int j=1;j<55;++j){
new_cards[orders[j]] = cards[j];
}
// 将new_cards的值赋值给cards
for(int j=1;j<55;++j){
cards[j] = new_cards[j];
}
}
for(int i=1;i<55;++i){
printf("%s",cards[i].c_str());
if(i<54) printf(" ");
}
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。