自测-5 Shuffling Machine (20分)
题目描述这里就不描述了
题目链接点我
思路
- 摆明了就是交换数组的元素咯,比如说,当前扑克牌顺序是2 3 4 5 1
- 而交换数组的顺序是3 2 1 5 4
- 这样就是2要换到第三个位置上,3要换到第二个位置上,4换到第一个位置上…这里要注意数组下标从零开始
- 于是乎,换完就变成了 4 3 2 1 5
- 不过,如果要换两次的话,记住是用第一次换完后的数组再进行交换(再来一次循环)
- 还有一个小技巧,就是不用一个char数组存扑克牌的顺序,太麻烦了…我们可以通过顺序判断输出就行了,比如50对应D13,以此类推
AC代码
#include <iostream>
using namespace std;
int dig_2_str(int n)
{
if(n <= 13)
return 0;
else if( n <= 26)
return 1;
else if(n <= 39)
return 2;
else if(n <= 52)
return 3;
else
return 4;
}
int main()
{
int n;
scanf("%d",&n);
int a[54];
for(int i=0;i<54;i++)
cin>>a[i];
int b[n+1][54];
for(int i=0;i<54;i++)
b[0][i] = i+1;
int c=0;
while(n)
{
c++;
for(int i=0;i<54;i++)
{
int k = a[i]-1;
b[c][k] = b[c-1][i];
}
n--;
}
int i=0;
for(;i<53;i++)
if(dig_2_str(b[c][i]) == 0)
printf("S%d ",b[c][i]);
else if(dig_2_str(b[c][i]) == 1)
printf("H%d ",b[c][i]-13);
else if(dig_2_str(b[c][i]) == 2)
printf("C%d ",b[c][i]-26);
else if(dig_2_str(b[c][i]) == 3)
printf("D%d ",b[c][i]-39);
else
printf("J%d ",b[c][i]-52);
if(dig_2_str(b[c][i]) == 0)
printf("S%d",b[c][i]);
else if(dig_2_str(b[c][i]) == 1)
printf("H%d",b[c][i]-13);
else if(dig_2_str(b[c][i]) == 2)
printf("C%d",b[c][i]-26);
else if(dig_2_str(b[c][i]) == 3)
printf("D%d",b[c][i]-39);
else
printf("J%d",b[c][i]-52);
return 0;
}