PAT 1042 shuffling machine 翻译 分析 代码

本文详细介绍了PAT 1042题目的洗牌程序,提供了一种使用C语言实现的解决方案。通过对给定的洗牌顺序进行多次操作,得到最终的卡片排列结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:1042 shuffling machine (20)(20 分)【洗牌程序

A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

【    花色介绍:{S,H,C,D,J}

       程序介绍:给出五张卡片S3, H5, C1, D13,J2,并且给出洗牌顺序{4,2,5,3,1},结果会变为{J2,H5,D13,S3,C1}

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (<= 20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.


【每个输入的文件都包括一个测试用例。在每个例子中,第一行都包括一个交换次数K,接下来的一行给出命令,输出行中的所有代表卡牌的数字都要被一个“ ”分开。】

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line

【对每个测试数据,在同一行输出洗牌结果,所有的代表卡牌的数字被“ ”空格分开,并且在行尾后不允许出现空格!】
【对上面这个要求的解读是,不允许出现printf("%c%d ,color[start[i]/13],start[i]%13+1);的格式,注意!!! %c%d后面的“空格”不允许存在!否则与输出要求不符!】

【注】:以下分析所在为代码注释,通过注释可以分析得到此题答案。

【注】:此代码是由Dev c++ 5.11 所写,运用c语言。  其他方法例如c++,后续会另写博客给出方式。惊讶

/**************************************************************************************************************************/

#include<stdio.h>
//#define P 54
int main()
{
char color[5]={'S','H','C','D','J'};	                //color为花色数组 ; 
int P=54;	//本题建议使用int 写死P的数值;define定义P值,会使程序整体运行时间变长  define 384ms  int 256ms 
int start[P],end[P],operate[P];	    	        //start为初始卡牌数组;end为操作后卡牌数组;operate为操作次序数组 ; 
int K,i;
for(i=0;i<P;i++)	//初始化填充start数组 ; 
{
start[i]=i+1;
}


scanf("%d",&K);	//输入变化的次数 


for(i=0;i<P;i++) //输入操作变化的数组 
{
scanf("%d",&operate[i]);
}	
/*****************数据检查*********************
for(i=0;i<P;i++)
{	//输出初始化的start[i]数组,检查是否初始化正确。运行时注释掉本段; 
printf("%d ",start[i]);
}	
for(i=1;i<=P;i++)	//运行时注释掉本段 
{
printf("%d ",operate[i]);
}
/**********************************************/


/***********************************主要函数***************************************/	


for(int step = 0;step < K; step++)             //执行K次操作——对start数组进行修改的次数 ; 
{
for(i=0;i<P;i++)
{
end[operate[i]-1]=start[i];	    //根据operate数组的指令交换指定位置的start与end; 
}
for(i=0;i<P;i++)
{
start[i]=end[i];                //再次交换start与end数组,使start数组成为进行了1次位置变化的数组,方便进行后续操作;	
} 
}
for(i=0;i<P;i++)
{
start[i]--;
if(i!=0)printf(" ");
printf("%c%d",color[start[i]/13],start[i]%13+1);	
}
 	return 0;
 } //唱晚,我爱你啊。 2018年7月2日17:40:13

/**********************************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值