算法导论第五章

来源:互联网 发布: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;}





1 0
原创粉丝点击