2014-04-29 01:02

题目:从m个整数里随机选出n个整数,要求等概率。

解法:和洗牌的算法类似,每次随机抽出一个数,抽n次即可。时间复杂度O(m * n),空间复杂度O(m)。

代码:

 // 18.3 pick m integers randomly from an array of n integer.
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std; void randomSubset(const vector<int> &src, vector<int> &dst, int m)
{
vector<int> v;
int i, j;
int idx;
int n; v = src;
n = (int)v.size();
dst.resize(m);
for (i = ; i < m; ++i) {
idx = rand() % n;
dst[i] = v[idx];
--n;
for (j = idx; j < n; ++j) {
v[j] = v[j + ];
}
}
v.clear();
} int main()
{
int i;
int n, m;
vector<int> src;
vector<int> dst;
int cc; cc = ;
while (scanf("%d%d", &n, &m) == ) {
++cc;
srand((unsigned)(time(NULL) * cc));
src.resize(n);
for (i = ; i < n; ++i) {
src[i] = i;
}
randomSubset(src, dst, m);
for (i = ; i < m; ++i) {
printf((i % == ? "%3d\n" : "%3d "), dst[i]);
}
printf("\n");
} return ;
}

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

  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. hdu-3449 Consumer---有依赖性质的背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3449 题目大意: fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒 ...

  2. 空间最短路径,BFS(POJ3278)

    题目链接:http://poj.org/problem?id=3278 #include <cstdio> #include <queue> #include <stri ...

  3. python-类对象以列表切片模式操作

    #类对象以列表切片模式操作 class Person: def __init__(self): self.cache=[] def __setitem__(self, key, value): #修改 ...

  4. war和war exploded的区别

    是选择war还是war exploded 这里首先看一下他们两个的区别: (1)war模式这种可以称之为是发布模式,看名字也知道,这是先打成war包,再发布: (2)war exploded模式是直接 ...

  5. Ext4.2 select 和 query 区别与联系

    Ext.query和Ext.select的作用是一致的,同是根据CSS选择符查找出一个或多个元素.区别在于返回类型上.分别是:query方法返回的是JavaScript标准的数组类型:select方法 ...

  6. inline-block问题

    1.两个相邻的inline-block元素,给inline-block元素设置max-width并且overflow:hidden;之后,相邻行内元素回向下偏移问题 给inline-block元素添加 ...

  7. C编程经验总结4

    {}体里的语句不管在一行还是在多行,之间都是要有: for与for之间可以是独立的,也可以是相互嵌套的 For( ; i<5; )=for( ;i<=4;  ) 一般都是在循环里面进行判断 ...

  8. pycharm快捷键一览

    编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl + P ...

  9. ntp网络时间服务搭建

    1.1 NTP简介 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议. 1.2 NTP用途 有些时候,局域网里面的设备需要进行时间的同步, ...

  10. python-含参函数

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- ''' #----------函数位置参数和关键字参数---------- def test(x,y): ...