A1042

    技术2025-11-10  27

    Shuffling Machine (20分)

    单词:

    shuffling:洗牌          position:位置the number at the i-th position is j, it means to move the card from position i to position j:第i个位置上是j,表示移动第i个位置上的数字→第j个位置。(即order[ i ] = j, 那么end[ j ]=start[ i ])。

    总结:

    利用char数组控制输出,char mp[5] = {'S', 'H', 'C', 'D', 'J'};或者char mp[6] = {"SHCDJ" };(字符串存储末尾'\0'),其中mp[0]='S',mp[1]='H'以此类推。用start和end数组保存每次变换的开始顺序和结束顺序(以1~54的编号存储)。每次变换令start[j]=end[i],end数组在第一次变换之前需要进行初始化。一次结束后需要对end数组进行更新(令end[ i ] = start[ i ]),便于下一次洗牌。输出思考1→S1,2→S2,......13→S1314→D1,...... 字母部分:不能简单认为是mp[ end[i]/13 ],要先让end[i]先-1/13。(错误:13/13=1,显示H)数字部分:取余数也不能直接%13,要先-1再%13最后+1。(错误:13%13=0,显示0)。

    代码:

    #include <cstdio> int main(){ int start[55],end[55], order[55]; char mp[5] = {'S', 'H', 'C', 'D', 'J'}; //0:'S', 1:'H', 2:'C', 3:'D' 4:'J' //或者char mp[6] = {"SHCDJ" }; int cnt; scanf("%d", &cnt); for(int i=1; i<55; i++){ scanf("%d", &order[i]); end[i] = i; //初始化 } while(cnt--){ for(int i=1; i<55; i++){ start[ order[i] ] = end[i]; } for(int i=1; i<55; i++){ end[i] = start[i]; } } for(int i=1; i<55; i++){ if( i!=1 ) printf(" "); end[i]--; //包括下一行 方便处理输出 printf("%c%d", mp[ end[i]/13 ], end[i]%13+1 ); } return 0; }

     

    Processed: 0.012, SQL: 9