编程珠玑
cattylll
这个作者很懒,什么都没留下…
展开
-
编程珠玑习题1.9分析和我的总结
题目: One problem with trading more space to use less time is that initializing the space can itself take a great deal of time. Show how to circumvent this problem by designing a technique to initialize原创 2011-12-09 14:18:00 · 1291 阅读 · 2 评论 -
编程珠玑第二章习题2
给定一个包含4300000000个32位整数的顺序文件,请问如何找到一个至少出现两次的整数?顺序文件,不允许随机访问。 解答:BinarySearch,但不是对文件内容折半,而是对搜索范围折半。由于4.3G>32位的整数空间,根据鸽笼原理,肯定会有重复的整数。搜索范围从所有的32位正整数开始(全部当成unsigned int,简化问题),即[0,2^32),中间值即为2^31。转载 2011-12-18 18:03:19 · 1326 阅读 · 2 评论 -
编程珠玑12章习题 12.9 FLoyd随机取样算法的证明(转自编程珠玑续)
13.3 随机排列一些使用随机样本的程序要求样本的元素以随机的顺序出现。这样的序列被称为无重复的随机排列。例如,在测试一个排序程序的时候,随机产生的输入必须以随机的顺序出现;如果输入总是有序的,那么可能不能充分地测试排序代码。我们可以利用Floyd算法F2产生一组随机样本,然后把它复制到一个数组中,最后打乱数组中元素的顺序。这段代码用于随机地打乱数组 的顺序:for I :=转载 2011-12-14 13:51:43 · 1140 阅读 · 0 评论 -
我写的编程珠玑第一章位图的实现,使用C++
[code C/C++]#include #include #include #define MAXN 2000000#include #include #include int x[MAXN];using namespace std;void produceRandomFile(string& path,int m,int n);#define MASK 31int randin原创 2011-12-14 15:31:42 · 409 阅读 · 0 评论 -
两种版本的二分搜索(递归+非递归)
递归版本int binarySearch(int i, int j,int *a, int find){ int m; if(i == j) { if(a[i] == find) return i; else return -1; //can't find this number in the array } else { m = (i+j)/2;原创 2011-12-29 14:48:04 · 578 阅读 · 0 评论 -
编程珠玑第四章习题
4-2 我写了两个,一个找到第一个相等的,一个找到最后一个相等的#include "stdafx.h"#include using namespace std;int BinarySearchLeast(int start, int end, int find, int* a );int BinarySearchLarge(int start, int end, int find, i原创 2011-12-31 17:14:57 · 990 阅读 · 1 评论 -
编程珠玑第三章习题
1.使用二分搜索来搜索大于等于income的最小的lower_limit#include "stdafx.h"#include using namespace std;int binarysearch(int start, int end, int find, int* a);int main(int argc, char* argv[]){ int lower_limit[原创 2011-12-27 14:00:11 · 1459 阅读 · 4 评论 -
生成0~n-1内的m个随机整数的四种算法
生成0~n-1内的m个随机整数的四种算法方法一:knuth的算法,例如n=5, m =2, 那么选第一个元素的概率就是2/5,如果选上了那么选第二个元素概率就是1/4,否则选择第二个元素的概率就是2/4void gen_knuth(int n, int m){ for(int i = 0; i < n; i ++) { if(big_rand()%(n-i) < m)原创 2012-01-04 22:15:55 · 2164 阅读 · 0 评论