方法一:借鉴别人的,满分通过版本
#include<stdio.h>
const int N = 54;
char mp[5] = { 'S', 'H', 'C', 'D', 'J'};
int start[N+1], end[N+1], next[N+1];
int main()
{
int K;
scanf("%d", &K);
for(int i = 1; i <= N; i++)
{
start[i] = i;
}
for(int i = 1; i <= N; i++)
{
scanf("%d", &next[i]);
}
for(int step = 0; step < K; step++)
{
for(int i = 1; i <=N; i++)
{
end[next[i]] = start[i];
}
for(int i = 1; i <= N; i++)
{
start[i] = end[i];
}
}
for(int i = 1; i<= N; i++)
{
if(i != 1) printf(" ");
start[i]--;
printf("%c%d",mp[start[i] / 13] , start[i] % 13 + 1);
}
return 0;
}
方法二:输出结果与答案完全一致,但是所有测试用例均显示答案错误,可能是每次循环都输出了’\0’的缘故。
#include<cstdio>
const int CARDS = 54;
int a[60] = {0};
int b[2][60] = {0};
char * getCard(int a){
char c[4];
char * result = c;
switch((a - 1) / 13){
case 0:c[0] = 'S';break;
case 1:c[0] = 'H';break;
case 2:c[0] = 'C';break;
case 3:c[0] = 'D';break;
case 4:c[0] = 'J';break;
}
int num = (a - 1) % 13 + 1;
if(num >= 10){
c[1] = '1';
c[2] = num - 10 + '0';
c[3] = '\0';
}else{
c[1] = num + '0';
c[2] = '\0';
}
return result;
}
int main(){
int k;
scanf("%d",&k);
for(int i = 1; i <= CARDS; i++){
scanf("%d",&a[i]);
}
for(int i = 0;i < k; i++){
if(i == 0){
for(int i = 1; i <= CARDS; i++){
b[0][a[i]] = i;
}
} else{
if(i % 2 == 0){
for(int i = 1; i <= CARDS; i++){
b[0][a[i]] = b[1][i];
}
}else{
for(int i = 1; i <= CARDS; i++){
b[1][a[i]] = b[0][i];
}
}
}
}
for(int i = 1; i <= CARDS; i++){
if(i != 1){
printf(" ");
}
printf("%s",getCard(b[1 - k % 2][i]));
}
return 0;
}
方法三:在方法二的基础上结合方法一最后的处理,满分通过
#include<cstdio>
const int CARDS = 54;
int a[60] = {0};
int b[2][60] = {0};
char mp[5] = { 'S', 'H', 'C', 'D', 'J'};
char * getCard(int a){
char c[4];
char * result = c;
switch((a - 1) / 13){
case 0:c[0] = 'S';break;
case 1:c[0] = 'H';break;
case 2:c[0] = 'C';break;
case 3:c[0] = 'D';break;
case 4:c[0] = 'J';break;
}
int num = (a - 1) % 13 + 1;
if(num >= 10){
c[1] = '1';
c[2] = num - 10 + '0';
c[3] = '\0';
}else{
c[1] = num + '0';
c[2] = '\0';
}
return result;
}
int main(){
int k;
scanf("%d",&k);
for(int i = 1; i <= CARDS; i++){
scanf("%d",&a[i]);
}
for(int i = 0;i < k; i++){
if(i == 0){
for(int i = 1; i <= CARDS; i++){
b[0][a[i]] = i;
}
} else{
if(i % 2 == 0){
for(int i = 1; i <= CARDS; i++){
b[0][a[i]] = b[1][i];
}
}else{
for(int i = 1; i <= CARDS; i++){
b[1][a[i]] = b[0][i];
}
}
}
}
for(int i = 1; i <= CARDS; i++){
if(i != 1){
printf(" ");
}
int index = b[1 - k % 2][i];
printf("%c%d",mp[(index - 1)/13],(index - 1)%13+1);
}
return 0;
}
|