扑克牌的顺子
2016-06-13 17:26
295 查看
//从扑克牌中随机抽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;
}随机抽牌的程序有点意思。
//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;
}随机抽牌的程序有点意思。
相关文章推荐
- BZOJ_1625_ [Usaco2007_Dec]_宝石手镯_(01背包)
- Android5.0 ripple效果低版本的实现
- actionbar(Android开源项目)
- Java加密技术(十)——单向认证
- 怎样阅读别人的安卓项目
- 通过adb录制视频并通过FFMPEG将MP4转换成GIF格式(二)
- <LeetCode OJ> 357. Count Numbers with Unique Digits
- 关于WEB的URL安全测试
- VMwareTools 安装(VMware Player)
- 构建之法阅读笔记04
- 构建之法阅读笔记六
- 解决Android中,禁止ScrollView内的控件改变之后自动滚动
- Android入门--Toast 显示一个ImageView
- 部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题
- Java加密技术(九)——初探SSL
- Android文件存储(一)
- Cubietruck---1. ct的使用说明烧写及源码的编译
- 常用模板(感觉还不错)
- IOS Dev Intro - Core Media
- 大数据:“人工特征工程+线性模型”的尽头