算法导论第五章
来源:互联网 发布:jdk 6u45 windows x64 编辑:程序博客网 时间:2024/06/05 23:42
5-1-2
题目:
Describe an implementation of the procedure RANDOM(a, b) that only makes calls to RANDOM(0, 1). What is the expected running time of your procedure, as a function of a and b?
#include "stdio.h"#include <iostream>#include <ctime>#include <math.h>using namespace std;int RandomSelf(int a,int b);int main(){ int i = 1; int a=1,b=100; //cin>>a>>b; while((1<<i)<(b-a)) i++; int result; int count[101]={}; for(int j = 0;j<20000000;j++) { do { result = RandomSelf(a,a+(1<<i)-1); }while(result<a || result>b); count[result]++; } for(int k=1;k<=100;k++) cout<<count[k]<<" ";}int RandomSelf(int a,int b){ if(a == b) return a; else { int mid = (a+b)/2; //srand(time(0)); int flag = rand()%2; if(flag == 0) return RandomSelf(a,mid); else return RandomSelf(mid+1,b); }}
运行统计结果:
5-1-3
Suppose that you want to output 0 with probability 1/2 and 1 with probability 1/2. At your disposal is a procedure BIASED-RANDOM, that outputs either 0 or 1. It outputs 1 with some probability p and 0 with probability 1 - p, where 0 < p < 1, but you do not know what p is. Give an algorithm that uses BIASED-RANDOM as a subroutine, and returns an unbiased answer, returning 0 with probability 1/2 and 1 with probability 1/2. What is the expected running time of your algorithm as a function of p?
int RANDOM(){ while(1) { int x = BASED-RANDOM; int y = BASED-RANDOM; if(x != y) return x; }}
5-3-6
Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the case in which two or more priorities are identical. That is, your algorithm should produce a uniform random permutation, even if two or more priorities are identical.
#include "stdafx.h"#include <stdio.h>#include <iostream>#include <string>#include <math.h>#include <time.h>using namespace std;void quickSort(int p[],int a[], int left,int right)//begin from 0{if(left == right)return;int x = left;int y = right;int mid = p[(left+right)/2];while(x < y){while(p[y] > mid)y--;while(p[x] < mid)x++;if(x <= y){int temp = p[x];p[x] = p[y];p[y] = temp;temp = a[x];a[x] = a[y];a[y] = temp;x++;y--;}}if(x < right) quickSort(p,a,x,right);if(y > left) quickSort(p,a,left,y);}int main(){srand(time(0));int a[10] = {1,2,3,4,5,6,7,8,9,10};int p[10];for(int i=0;i<10;i++){printf("%5d",a[i]);}cout<<endl;for(int i=0;i<10;i++){p[i]= rand()%(int)pow((double)10,(int)3);printf("%5d",p[i]);}cout<<endl;quickSort(p,a,0,9);//检查p中是否有相同的元素 int begin,end;begin = -1;for(int i=0;i<9;i++){if(p[i] == p[i+1]){if(begin == -1)begin = i;}else{if(begin != -1){end = i;for(int i=begin;i<=end;i++){p[i]= rand()%(int)pow((double)10,(int)3);}quickSort(p,a,begin,end);//重新从出现相同的数的地方开始扫描i = begin-1;//每次for循环完了开始之前会对i加1begin = -1;}}}for(int i=0;i<10;i++){printf("%5d",a[i]);}cout<<endl;return 0;}
- 算法导论: 第五章
- 算法导论第五章
- 算法导论第五章总结
- 算法导论读书笔记 第五章
- 算法导论第五章5.3随机算法
- 算法导论 第五章:随机算法
- 《算法导论》第一章~第五章 要点笔记
- 算法导论第五章学习笔记
- 算法导论第三版第五章思考题
- 《算法导论》第五章-思考题(参考答案)
- 算法导论读书笔记 第五章 概率分析和随机算法
- 算法导论第五章:概率分析和随机算法
- 《算法导论》学习笔记 第四章&&第五章
- 算法导论第五章5.1雇佣问题课后答案研究
- 算法导论学习笔记-第五章-概率分析和随机算法
- 算法导论第五章概率分析和随机算法最后思考题
- 算法导论——第五章概率分析与随机算法笔记
- 算法导论_第五章_概率分析和随机算法
- spring类扫描注入-----类扫描的注解解析器
- 移动应用实战(移动OA)之三_客户端与服务端数据通讯
- 在OpenWrt上移植fcgiwrap为nginx提供cgi支持
- SQL Server 根据表名获取表的所有列及属性(数据同步服务数据分析需要)
- 从热更新到Nuwa源码分析
- 算法导论第五章
- Redis安装配置教程
- 安卓开发不得不收集的工具类
- 一个管理nginx和php-fpm的脚本
- Struts环境搭建
- Data URI scheme - 数据的uri模式
- RunLoop
- Java系列-集合框架理解
- 如何建设一个营销型网站