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中通过编写C#程序调用dataloadercliq的bat文件取触发调用data loader来批量处理数据

    通过这篇文章 http://www.cnblogs.com/mingmingruyuedlut/p/3413903.html 我们已经知道了Data Loader可以对Salesforce的Objec ...

  2. ArrayList集合&特殊集合

    一.ArrayList集合 集合内可以放不同类型的元素 另:object类型为所有数据类型的基类 添加元素:.add(); 清空集合:al.clear(); 克隆集合:.clone(); 判断是否包含 ...

  3. mysql LAST_INSERT_ID 使用与注意事项

    在使用MySQL时,若表中含自增字段(auto_increment类型),则向表中insert一条记录后,可以调用last_insert_id()来获得最近insert的那行记录的自增字段值 $mdb ...

  4. poj 1321 棋盘问题

    八皇后问题变形,回溯法. #include <cstdio> #include <cstring> #include <iostream> using namesp ...

  5. shell-bash学习03 别名、日期、函数

    别名 使用alias 创建 alias new_command='command sequence' 保存 echo 'alias cmd="command seq"' >& ...

  6. 选项卡 tab切换

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  8. Codeforces 467C George and Job(DP)

    题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...

  9. HDU1853 Cyclic Tour(最小费用最大流)

    题目大概说给一张有向图,每条边都有权值,要选若干条边使其形成若干个环且图上各个点都属于且只属于其中一个环,问选的边的最少权值和是多少. 各点出度=入度=1的图是若干个环,考虑用最小费用最大流: 每个点 ...

  10. MongoDB 入门之基础 DCL

    此文章主要记录部分主要的 MongoDB 的 DCL 操作. MongoDB 默认不需要用户名和密码就可以用 mongodb.exe 登录 一.开启 MonogoDB 的权限模式 修改 MongoDB ...