PAT甲级 1042 Shuffling Machine (20分)

1042 Shuffling Machine (20分)

题目链接:PAT A 1042
在这里插入图片描述
题目大意:有54张扑克牌,初始时排列顺序分别是S1…S13…H1…H13…C1…C13…D1…D13…J1 J2,现在给出一个重复洗牌次数k,然后给出54个数字,每个位置i上都放置了一个数字,初始时S1…J2在位置1,2…54上。对于每次洗牌,将位置i上的牌移动到位置i放置的数字所代表的位置上。例如有3张牌a,b,c,初始时a在位置1,b在位置2,c在位置3,洗牌序列是3,1,2,重复两次。那么第一次重复是将a移动到位置3,将b移动到位置1,将c移动到位置2,即变为b,c,a,第二次重复是将b移动到位置3,将c移动到位置1,将a移动到位置2,即变为c,a,b。现在要求输出重复k次后位置1-54上分别放置了哪张牌。

思路分析:定义一个str数组,下标从0到4分别存储S-J。之后输入洗牌序列用数组num存储,注意第一张牌代表S1,第二张牌代表S2,以此类推,遍历54张牌。定义一个temp变量记录经过k次洗牌时第i张牌所在的位置,初始时将temp赋值为i,每次洗牌都让temp = num[temp],最后end[temp] = i,即第i张牌放置在了位置temp上,遍历完后end[1]就代表第一个位置上是哪张牌,例如end[1]的值是12,就意味着第一个位置是S12。之后再次遍历54张牌,将end[i]的值自减(主要是方便输出),之后分别用/13和%13 + 1的方式去输出对应的字母和数字即可~

#include<iostream>
using namespace std;
int main() {
	char str[6] = "SHCDJ";
	int k, num[55], end[55];
	scanf("%d", &k);
	for(int i = 1; i <= 54; i++) //输入洗牌序列 
		scanf("%d", &num[i]);
	for(int i = 1; i <= 54; i++) {
		int temp = i; //temp记录第i张牌最终放置在哪 
		for(int j = 0; j < k; j++) 
			temp = num[temp];
		end[temp] = i; //第i张牌放置在位置temp上 
	}
	for(int i = 1; i <= 54; i++) {
		end[i]--; //自减方便输出 
		printf("%c%d", str[end[i] / 13], end[i] % 13 + 1);
		if(i != 54)
			printf(" ");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A shuffling machine in C++ can be implemented using an array to represent the deck of cards and using the random number generator to shuffle the cards. Here is a sample code for a shuffling machine: ``` #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int NUM_CARDS = 52; class ShufflingMachine { private: int deck[NUM_CARDS]; int position; public: ShufflingMachine() { for (int i = 0; i < NUM_CARDS; i++) { deck[i] = i; } position = 0; } void shuffle() { srand(time(NULL)); for (int i = 0; i < NUM_CARDS; i++) { int j = rand() % NUM_CARDS; swap(deck[i], deck[j]); } position = 0; } int dealCard() { if (position >= NUM_CARDS) { shuffle(); } return deck[position++]; } }; int main() { ShufflingMachine shuffler; shuffler.shuffle(); for (int i = 0; i < NUM_CARDS; i++) { cout << shuffler.dealCard() << " "; } cout << endl; return 0; } ``` In this code, the `ShufflingMachine` class represents the shuffling machine. The `deck` array stores the deck of cards, and the `position` variable keeps track of the current position in the deck. The `shuffle` method shuffles the deck by randomly swapping cards. It uses the `srand` function to seed the random number generator with the current time, and the `rand` function to generate random indices for swapping cards. The `dealCard` method deals the next card from the deck. If the deck has been exhausted, it calls the `shuffle` method to shuffle the cards again. In the `main` function, we create a `ShufflingMachine` object and shuffle the cards. Then we deal all the cards and print them out.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值