2014-04-29 00:59

题目:设计一个洗牌算法,效率尽量快点,必须等概率。

解法:每次随机抽一张牌出来,最后都抽完了,也就洗好了。时间复杂度O(n^2),请看代码。

代码:

 // 18.2 shuffle a deck of 52 cards, it must be perfect random.
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std; void printCards(const vector<int> &cards)
{
int i;
int n = (int)cards.size();
const int col = ; for (i = ; i < n; ++i) {
printf((i % col == col - ? "%4d\n" : "%4d "), cards[i]);
}
printf("\n");
} void shuffleCards(vector<int> &cards)
{
vector<int> v; v = cards;
int i, j;
int n, n0;
int idx; n0 = n = (int)cards.size();
for (i = ; i < n0; ++i) {
idx = rand() % n;
cards[i] = v[idx];
--n;
for (j = idx; j < n; ++j) {
v[j] = v[j + ];
}
} v.clear();
} int main()
{
srand((unsigned)time(NULL));
vector<int> cards;
int i;
const int n = ; cards.resize(n);
for (i = ; i < n; ++i) {
cards[i] = i;
} shuffleCards(cards);
printCards(cards); return ;
}

《Cracking the Coding Interview》——第18章:难题——题目2的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

随机推荐

  1. C#调用C++的dll存在的问题

    C#调用C++写的DLL时,在C#程序中,使用DllImport定义C++导出函数的定义信息,之前在C++中定义导出函数时,使用了long作为参数类型,使用C#调用时,开始在64位版本是哪个测试,一切 ...

  2. 【js基础修炼之路】--创建文档碎片document.createDocumentFragment()

          讲这个方法之前,我们应该先了解下插入节点时浏览器会做什么.         在浏览器中,我们一旦把节点添加到document.body(或者其他节点)中,页面就会更新并反映出这个变化,对于 ...

  3. 起一个node服务

    使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...

  4. 【转】startActivityForResult和setResult详解

    startActivityForResult与startActivity的不同之处在于:1.startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startAct ...

  5. php图像处理插件imagick安装(仅适用于86位,php5.4非安全环境-16px)

    phpImageMagick-6.7.7-5-Q16-windows-dll(加测试代码,经测试,仅适用于86位,php5.4安全环境-16px) 下载地址:http://pan.baidu.com/ ...

  6. 剑指offer54 表示数值的字符串

    错误的代码: class Solution { public: bool isNumeric(char* string) { if(string == NULL) return false; if(* ...

  7. Mac安装protobuf 流程

    下载 https://github.com/google/protobuf/releases 找到对应版本下载 编译 cd protobuf./autogen.sh./configuremake 安装 ...

  8. CPP-STL:用vector保存对象时保存指针的优点, 以及reserve的使用(转)

    代码1 #include <vector> #include <stdio.h> class A { public: A() { printf("A()/n" ...

  9. js实现弹窗一个ip在24小时只弹出一次的代码

    function cookieGO(name) { var today = new Date(); var expires = new Date(); expires.setTime(today.ge ...

  10. C# foreach语句

    一.C# foreach语句 foreach语句能够对实现Ienumerable接口的容器进行遍历,并提供一个枚举器来实现Ienumerable接口.foreach语句为数组或对象集合中的各个元素执行 ...