18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of the 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect.

这道题让我们实现一个洗牌的算法,实际上洗牌的原理非常简单,就是每张牌跟一个随机位置的牌互换位置即可,那么这里就有两种实现方法,递归和迭代。我们先来看递归的方法,具体来说用的是回溯的思想,我们从i=51开始,然后每次进入递归函数,到i=0时返回,然后到了i=1,然后跟[0,i]之间的随机一张牌交换位置,然后一层一层的回来,一直回到i=51,这样每张牌我们都随机交换过位置,从而达到了洗牌的目的:

解法一:

void shuffle(vector<int> &cards, int i) {
if (i == ) return;
shuffle(cards, i - );
int k = rand() % (i + );
swap(cards[k], cards[i]);
}

我们也可以用迭代的方法来实现,用一个for循环来将每张牌和随机位置的牌交换位置即可:

解法二:

void shuffle(vector<int> &cards) {
for (int i = ; i < cards.size(); ++i) {
int k = rand() % (i + );
swap(cards[k], cards[i]);
}
}

CareerCup All in One 题目汇总

[CareerCup] 18.2 Shuffle Cards 洗牌的更多相关文章

  1. Shuffle(洗牌)

    Shuffle(洗牌)    图    map        1.Map Task的输出k v,一开始会进入溢写缓冲区中,对数据做处理,比如分区.排序等操作.        2.有几个Map Task ...

  2. 文本数据挖掘---课后作业shuffle函数洗牌C++

    题目: 代码如下:#include <iostream> #include <random> #include <algorithm> #include <v ...

  3. 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)

    CARDS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1448   Accepted: 773 Description ...

  4. 闲话shuffle(洗牌)算法

    工作中经常会用到洗牌算法,看到这篇文章不错,原文摘自:http://www.atatech.org/article/detail/11821/928  作者:子仲   场景 洗牌算法的应用场景其实很多 ...

  5. uva 10710 - Chinese Shuffle(完美洗牌)

    option=com_onlinejudge&Itemid=8&category=474&page=show_problem&problem=1651"> ...

  6. 2.1 shuffle sort(洗牌)

    1.目的:将数组以随机的顺序重新排序,类似洗牌的过程 2.用途用于快速排序或者任何以划分为基础的排序中,目的是减少最坏可能性发生的概率. 3.想法1:给数组的每一个元素产生一个随机的数字作为键,然后使 ...

  7. [LeetCode] Advantage Shuffle 优势洗牌

    Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...

  8. [CareerCup] 18.3 Randomly Generate Integers 随机生成数字

    18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element m ...

  9. js 随机数 洗牌算法

    function shuffle(arr){ var len = arr.length; for(var i = 0;i<len -1;i++) { var idx = Math.floor(M ...

随机推荐

  1. 如何在Salesforce中进行代码开发

    两种方式: 1):用Salesforce自带的在线开发模式 Setup --> App Setup --> Develop --> than you can select 'Page ...

  2. 【HTML5 video】video标签的部分属性解析

    转自:http://www.cnblogs.com/kiter/archive/2013/02/25/2932157.html 现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Th ...

  3. 【MySQL 忘记密码】MySQL忘记密码怎么解决 mysql5.5 windows7

    ---恢复内容开始--- 如果MySQL 长久不使用,忘记密码,怎么解决??? 1.首先,需要在任务管理器关闭mysql相关的服务进程 2.cmd,进入DOS窗口,进入到mysql的安装路径的bin目 ...

  4. J2EE中使用jstl报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar错

    一.发现问题 运行引用了jstl的jsp页面 报http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or th ...

  5. 廖雪峰js教程笔记7 基本类型和包装类型

    在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...

  6. JDK BIO编程

    网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请求,通过三次握手建 ...

  7. 解决Kali Linux没有声音

    解决Kali Linux没有声音   Kali Linux系统默认状态下,root用户是无法使用声卡的,也就没有声音.启用的方法如下: (1)在终端执行命令:systemctl --user enab ...

  8. CSS3-样式继承,层叠管理,文本格式化

  9. zoj 3469 Food Delivery 区间dp + 提前计算费用

    Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving problems, we usual ...

  10. jquerymobile 基础教程

    http://www.w3cplus.com/blog/tags/331.html?page=1