Programming Challenges 习题 2.8.5

本文介绍了一种基于洗牌算法的卡牌迭代方法,通过使用C++实现,展示了如何根据特定的洗牌规则对一副标准的52张扑克牌进行重新排序。算法首先读取洗牌次数和每次洗牌的顺序,然后根据这些输入迭代更新卡牌位置,最终输出每张牌的名称和花色。

PC/UVa:110205/10205

Stack 'em Up

根据dealer的洗牌方法迭代。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
	int n = 0;
	cin >> n;
	cin.get();
	cin.get();
	for (int k = 0; k < n; k++)
	{
		int cnt = 0, suit = 0, num = 0;
		unsigned long idx;
		cin >> cnt;
		vector<vector<int>> viiShuffle(cnt, vector<int>(52, 0));
		vector<int> viInput(52, 0), viOutput(52, 0);
		for (int i = 0; i < cnt; i++)
		{
			for (int j = 0; j < 52; j++)
			{
				cin >> viiShuffle[i][j];
				viInput[j] = j;
			}
		}
		cin.get();
		string strIdx;
		while (getline(cin, strIdx)){
			if (strIdx.empty()) break;
			idx = stoul(strIdx);
			for (int j = 0; j < 52; j++)
			{
				viOutput[j] = viInput[viiShuffle[idx - 1][j] - 1];
			}
			viInput.assign(viOutput.begin(), viOutput.end());
		}
		for (int j = 0; j < 52; j++)
		{
			suit = viOutput[j] / 13;
			num = viOutput[j] % 13;
			if (num >= 0 && num <= 8) cout << num + 2;
			else if (num == 9) cout << "Jack";
			else if (num == 10) cout << "Queen";
			else if (num == 11) cout << "King";
			else if (num == 12) cout << "Ace";
			cout << " of ";
			if (suit == 0) cout << "Clubs" << endl;
			else if (suit == 1) cout << "Diamonds" << endl;
			else if (suit == 2) cout << "Hearts" << endl;
			else if (suit == 3) cout << "Spades" << endl;
			//cout << viOutput[j] << ' ';
		}
		if (k != n - 1)cout << endl;
	}
	return 0;
}
/*
1

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2
*/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值