//从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。
//2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王可以看成任意数字。
#include "iostream"
using namespace std;
//判断,大王小王用0表示 {0, 0, 1(A), 2~10, 11(J), 12(Q), 13(K)}
const int maxLength = 13; //牌的不一样的数最多13个
const int pickNum = 5; //抽取张数,可以改为其他数,增加了程序的扩展性
bool isContinuous(int* nums, int length)
{
int poker[maxLength + 1] = { 0 };
for (int i = 0; i < length; i++)
{
cout << nums[i] << endl;
poker[nums[i]]++;
}
int start = 1;
while (poker[start] == 0)
start++;
int end = start + length;
for (int i = start; i < end; ++i)
{
if (poker[i] == 0)
{
if (poker[0] > 0)
poker[0]--;
else
return false;
}
}
return true;
}
//一个随机抽牌的程序
#include "algorithm"
#include "chrono"
#include "random"
const int allPockerNum = 54;
void pick(int* pickPocker)
{
int allPocker[allPockerNum];
for (int i = 0; i < allPockerNum; ++i)
allPocker[i] = i + 1;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
shuffle(allPocker, allPocker+allPockerNum, std::default_random_engine(seed)); //如果这里去掉seed,<span style="font-family: Arial, Helvetica, sans-serif;">default_random_engine()则每次随机数产生一致</span>
for (int i = 0; i < pickNum; ++i)
{
if (allPocker[i] > 52)
pickPocker[i] = 0;
else
pickPocker[i] = allPocker[i] % 13; //乱序后的前5个
}
}
void test()
{
//int num[pickNum] = { 0, 0, 6, 5, 10 };
int num[pickNum];
pick(num);
cout << boolalpha << isContinuous(num, pickNum);//设置输出为bool型
}
int main()
{
test();
return 0;
}
随机抽牌的程序有点意思。