前段时间参加了下蘑菇街的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;}
}

蘑菇街三人斗地主随机发牌的笔试题相关推荐

  1. 集合三人斗地主的思路

    三人斗地主  集合完成 编写一个自动发牌程序,模拟三人斗地主的摸牌场景.首先要给出提示,谁首先开始摸牌,并且摸牌要和现实摸牌一样,三人循环摸牌,最后还要剩余三张底牌,同时给出地主牌,摸到地主牌的玩家拥 ...

  2. Python 三人斗地主手牌生成

    #三人斗地主手牌生成 import randomlst = list(range(2,11))+['J','Q','K','A'] colors = ['♥', '♦', '♣', '♠'] joke ...

  3. 复盘:一副牌(54张),三人斗地主,大小王在同一家的概率是多少

    复盘:一副牌(54张),三人斗地主,大小王在同一家的概率是多少? 提示:系列被面试官问的问题,我自己当时不会,所以下来自己复盘一下,认真学习和总结,以应对未来更多的可能性 关于互联网大厂的笔试面试,都 ...

  4. java斗地主随机发牌_java实现斗地主发牌功能

    本文实例为大家分享了java实现斗地主发牌的具体代码,供大家参考,具体内容如下 参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色:三名玩家,其中地主比其他玩家多3张牌) 代码如下: ...

  5. java斗地主随机发牌_使用Java实现斗地主游戏的发牌过程

    package com.review.test; import java.util.ArrayList; import java.util.Collections; import java.util. ...

  6. 用ArrayList 模拟三人斗地主

    public class Test { public static void main(String[] args) { //创建一个ArrayList 集合 保存 所有的牌 ArrayList< ...

  7. 利用JAVA中HashSet制作三人斗地主

    一.存牌 HashMap<Integer,String> hm = new HashMap<Integer,String>(); ArrayList<Integer> ...

  8. shuffle什么意思?python模拟随机发牌(斗地主、掼蛋)

    shuffle什么意思? 除了翻译成"拖曳摇摆地走;搅乱;推诿,推卸",还有洗牌的意思,随机但不重复. Shuffle the cards and deal out five to ...

  9. Java工程师笔试题整理[校招篇]

    Java工程师笔试题整理[校招篇] 阿里巴巴 2016 阿里巴巴2016研发工程师笔试题(四) 阿里巴巴2016研发工程师笔试题(三) 阿里巴巴2016研发工程师笔试题(二) 2015 阿里巴巴201 ...

  10. 建议收藏|四大互联网经典链表笔试题

    猫哥导读:随着大数据行业的发展,求职的道路上也会略显坎坷,公司对员工的要求也会越来越高,如果你在面试的路上没有碰壁过,只能说你太幸运了,我身边的朋友,基本上都会遇到面试失败的情况,大多数技术牛人都卡在 ...

最新文章

  1. python装饰器作用-python 装饰器
  2. oracle编程艺术笔记1
  3. 《Adobe Photoshop CC经典教程(彩色版)》—第4课4.7节添加调整图层
  4. 解决Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array.
  5. python 有效的字母异位词
  6. es6笔记 day3---对象简介语法以及对象新增
  7. C++ 虚函数与纯虚函数
  8. Mono for Android 显示远程图片
  9. 幻想和欲望毁了多少苦命的人(修改增加版)
  10. Linux 中/etc/profile、~/.bash_profile 环境变量执行过程
  11. 基于Netty实现群聊功能
  12. 网页端接入海康监控摄像头画面
  13. 如何下载朝阳区卫星地图高清版大图
  14. 量子计算机中的物理知识,量子计算机和物理学上的量子力学关系大吗?
  15. java 矩阵求逆_详解用java描述矩阵求逆的算法
  16. Kaggle比赛心得
  17. 怎么把网页添加到公众号里
  18. 上帝的心态发生了改变---太原市-------从热血沸腾到麻木不仁
  19. 我的世界java版_我的世界Java版1.16.5
  20. 如何计算感受野(Receptive Field)

热门文章

  1. ae预览绿条不完整_AE不能预览全部视频的原因分析及解决方案
  2. 自从我使用HiFlow场景连接器后,在也不用担心成为“落汤鸡”了
  3. ROS——在Ubuntu18.04下基于ROS Melodic编译python3的cv_bridge
  4. 手机APP神器大全,这些被堪称神器的APP你用过哪些!
  5. sublimit 编辑器扩展_视频字幕编辑工具-字幕编辑器(Subtitle Edit)下载v3.5.17 多语中文版-西西软件下载...
  6. mysql干嘛的_mysql和sql是干什么的?
  7. 学习python爬虫-爬取豆瓣top250相关信息
  8. 网络层(四)划分子网
  9. Vue入门(Vue.js,库与框架,MVVM,BootCDN,Vue入门,数据双向绑定,vue事件,生命周期,钩子函数 )
  10. 【前端静态资源托管库-CDN】BootCDN资源全线失效