蘑菇街三人斗地主随机发牌的笔试题
来源:互联网 发布:淘宝好看的高跟鞋店铺 编辑:程序博客网 时间:2024/04/29 05:18
前段时间参加了下蘑菇街的2014校园招聘的笔试题(PS我不是2014届的,只是去看看)。有一道笔试题大体是三人斗地主的游戏,将一副扑克牌随机分发给三个人,题目好像没有要求最后留三张牌给地主,所以全部发完就行。当时没做好,今天看STL剖析的时候看到了random_shuffle()这个算法,它提供按随机次序重新排列元素的功能,所以就用这个写了一下。
函数原型:random_shuffle(begin, end);
random_shffle(begin, end, rand);
第二个版本接受随机数发生器,是以引用传递的函数对象。
poker.h
#ifndef POKER#define POKER#include <iostream>#include <algorithm>#include <ctime>using namespace std;class PlayPoker{public:PlayPoker();~PlayPoker();void SendPokerToThreePlayers();void Display();private:void PrintPoker(int person[], int size);int poker[54];int player1[18];int player2[18];int player3[18];};#endif
poker.cpp
#include "Poker.h"PlayPoker::PlayPoker(){int index = 0;for(int i=1; i<=13; ++i){for(int j=0; j < 4; ++j){poker[index++] = i;}}poker[index] = 14;poker[index+1] = 15;}PlayPoker::~PlayPoker(){}void PlayPoker::SendPokerToThreePlayers(){srand(unsigned int(time(NULL)));int size = sizeof(poker) / sizeof(*poker);random_shuffle(poker, poker+size);int j = 0;for(int i=0; i < size; i += 3){player1[j] = poker[i];player2[j] = poker[i+1];player3[j] = poker[i+2];j++;}}void PlayPoker::PrintPoker(int player[], int size){for(int i=0; i < size; ++i){cout<<player[i]<<" ";}cout<<endl;}void PlayPoker::Display(){cout<<"Total Pokers:"<<endl;PrintPoker(poker, sizeof(poker)/sizeof(*poker));cout<<endl;cout<<"Person1's Poker:"<<endl;PrintPoker(player1, sizeof(player1)/sizeof(*player1));cout<<endl;cout<<"Person2's Poker:"<<endl;PrintPoker(player2, sizeof(player2)/sizeof(*player2));cout<<endl;cout<<"Person3's Poker:"<<endl;PrintPoker(player3, sizeof(player3)/sizeof(*player3));cout<<endl;}
main.cpp
#include "Poker.h"using namespace std;int main(){PlayPoker play;play.SendPokerToThreePlayers();play.Display();return 0;}
第一次运行结果:
第二次运行结果:
不知道这样是否符合题目要求。
后续:在C和指针上看到一种使用随机数洗牌的方法,可以替代random_shuffle(); 对比random_shuffle()的源码,其实现基本一致。
void PlayPoker::Shuffle(int poker[], int size){for(int i=size-1; i > 0; --i){int index;int temp;index = rand() % i; //产生序列的随机坐标/*当前坐标的元素和随机生成的坐标的元素交换*/temp = poker[index];poker[index] = poker[i];poker[i] = temp;}}
0 0
- 蘑菇街三人斗地主随机发牌的笔试题
- 斗地主随机发牌案例
- 三人斗地主技巧
- 斗地主发牌算法
- 斗地主:洗牌发牌
- 斗地主发牌
- java 写一个可以给斗地主玩家随机发牌的程序。
- 扑克牌的发牌功能,存储,以及显示,斗地主发牌
- 三人斗地主基础玩法
- 斗地主 发牌核心 代码
- 实现斗地主发牌(java)
- 斗地主发牌小程序
- 斗地主洗牌+发牌+排序
- Java模拟斗地主发牌
- cocos斗地主发牌动画
- Java-斗地主洗牌发牌
- Java简易斗地主发牌
- 模仿斗地主游戏发牌
- 一个linux控制台程序demo
- UINavigationController 设置左右按钮
- Linux设备驱动程序学习(3)-并发和竞态
- ios开发答疑录系列---xcode release 和 debug 模式造成的不同
- 解决boost::asio的WinSock.h has already been included
- 蘑菇街三人斗地主随机发牌的笔试题
- Linux常用命令征集
- 常见端口列表
- VS2013 Website publish
- 引用Log4net.dll的模块,无法打开
- MFC三种消息映射的区别与联系
- Java注释@interface的用法
- STAF中的STAFObject结构体
- linux用scp上传文件时出现错识 lost connection