重复生成52个随机数,并映射为每张扑克牌
题目:编写函数,重复生成52个随机数,并映射为每张扑克牌。说明:重复生成的典型原则是按照花色(梅花、方块、红桃、黑桃)和大小(2~10、J、Q、K、A)顺序进行映射,例如梅花2小于梅花3,……,梅花A小于方块2,……,黑桃K小于黑桃A。需要注意的是,一旦生成某张牌后,即不允许再次生成它,如何解决此问题?
1 思路
Idea A
- 先生成
[1, 52]
之间的随机数52个; - 找出 52 个随机数中的缺失部分和重复部分;
- 将重复部分替换成缺失部分的元素;
- 将更新后的不重复的 52 个随机数,进行发牌:
[2, ..., 10, J, Q, K, A]
为扑克牌大小顺序;- 花色大小:
梅花 - C (club) < 方块 - D (Diamond) < 红桃 - H (Heart) < 黑桃 - S (Spade)
; - 随机数范围:
[1, 13]
—— 梅花; - 随机数范围:
[14, 26]
—— 方块; - 随机数范围:
[27, 39]
—— 红桃; - 随机数范围:
[40, 52]
—— 黑桃。
Idea B
- 先生成
[1, 52]
之间的随机数52个; - 每生成一个进行花色和重复检测;
- 对重复的进行同位置或者(其他位置)替换;
code - Idea A 基础练习,无任何高级函数和算法
#include <iostream>
#include <ctime> /* 调用time, 用srand()生成种子 */
#include <cstdlib> /* rand() */
using namespace std;
void welcome();
void Randomize();
int getRandomNum( int low, int high ); /* 获取[low, high]区间内的随机数 */
void sortBubble(int a[], int n); /* 利用冒泡排序,方便查找缺失值和重复值 */
void showPoker(int vaule, int color); /* 显示函数 */
int main()
{
int i, color, temp;
int len = 0, l = 0, j = 0, k;
int pokerVaule[52] = {0};
int repeatVaul[52] = {0};
int defectVaul[52] = {0};
int sortPoker[52] = {0};
/* 生成 52 个 [1, 52] 之间的随机数 */
// cout << "||| 生成 52 个 [1, 52] 区间的随机数|||" << endl;
for (i = 0; i < 52; i++)
{
pokerVaule[i] = getRandomNum(1, 52);
sortPoker[i] = pokerVaule[i];
}
/* 排序 */
sortBubble(sortPoker, 52);
// Test
// cout << "||| 对生成的52个随机数进行升序排序 |||" << endl;
// for (i = 0; i < 52; i++)
// cout << sortPoker[i] << " ";
// cout << endl;
/* 找出重复的值 */
for (i = 0; i < 50; i++)
{
if ( (sortPoker[i+1] == sortPoker[i]) && (sortPoker[i+1] != sortPoker[i+2]) ){
repeatVaul[l] = sortPoker[i+1];
l++;
}
}
// cout << "||| 提取 52 个随机数中-重复生成的随机数 |||" << endl;
// for (i = 0; i < l; i++)
// cout << repeatVaul[i] << " ";
// cout << endl;
/* 提取缺少的值 */
if ( sortPoker[0] > 1){
int flag = sortPoker[0] - 1;
while (flag > j){
defectVaul[j] = j + 1;
j++;
}
}
for (i = 0; i < 51; i++)
{
int k = 1;
if ( (sortPoker[i + 1] - sortPoker[i]) > 1 ){
int flag = sortPoker[i + 1] - sortPoker[i];
while (flag > k){
defectVaul[j] = sortPoker[i] + k;
k++;
j++;
}
}
}
// cout << "||| 查找 52 个随机数在区间[1, 52]丢失的数 |||" << endl;
// for (i = 0; i < j; i++)
// cout << defectVaul[i] << " ";
// cout << endl;
/* 查找重复的位置 */
/* 替换后面重复的值为缺损的值 */
for (i = 0; i < 52; i++)
{
if (repeatVaul[i] != 0)
len++;
}
j = 0;
for (i = 0; i < len; i++)
{
int num = 0;
for (k = 0; k < 52; k++)
{
if (repeatVaul[i] == pokerVaule[k]){
// repeatLoca[m] = k;
num++;
// m++;
if (num > 1){
pokerVaule[k] = defectVaul[j];
j++;
}
}
}
}
welcome();
for (i = 0; i < 52; i++)
{
cout << " 第 " << i+1 << " 次[发牌]" << " ";
temp = pokerVaule[i];
if (temp <= 13 && temp >= 1 ){
color = 1;
showPoker(temp, color);
}
else if (temp <= 26 && temp >= 14){
color = 2;
temp -= 13;
showPoker(temp, color);
}
else if (temp <= 39 && temp >= 27){
color = 3;
temp -= 26;
showPoker(temp, color);
}
else{
color = 4;
temp -= 39;
showPoker(temp, color);
}
}
return 0;
}
void welcome()
{
cout << "|||| [Poker] - Test Deal ||||" << endl;
cout << "||||------------------------||||" << endl;
cout << "||||------ ^_^ ^_^ -------||||" << endl;
cout << "|||| 黑桃:S (Spade) ||||" << endl;
cout << "|||| 红桃:H (Heart) ||||" << endl;
cout << "|||| 方块:D (Diamond) ||||" << endl;
cout << "|||| 梅花:C (Club) ||||" << endl;
cout << "||||------ ^_^ ^_^ -------||||" << endl;
}
void Randomize()
{
srand( (int)time(0) );
}
int getRandomNum( int low, int high )
{
double _d;
if (low > high)
{
cout << "Generate Random number: Make sure low <= high.\n";
exit(1);
}
_d = (double)rand() / ( (double)RAND_MAX + 1.0 );
return low + (int)( _d * (high - low + 1) );
}
void sortBubble(int a[], int n)
{
int i, temp;
int low = 0, high = n - 1;
while (low < high)
{
for (i = 0; i < high; i++)
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
high--;
for (i = high; i > low; i--)
{
if (a[i] < a[i - 1])
{
temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
}
}
low++;
}
}
void showPoker(int vaule, int color)
{
if (color == 1){
cout << "Club " << " ";
}
else if (color == 2){
cout << "Diamond" << " ";
}
else if (color == 3){
cout << "Heart " << " ";
}
else{
cout << "Spade " << " ";
}
if (vaule == 11){
cout << "J" << endl;
}
else if (vaule == 12){
cout << "Q" << endl;
}
else if (vaule == 13){
cout << "K" << endl;
}
else if (vaule == 1){
cout << "A" << endl;
}
else{
cout << vaule << endl;
}
}