ex1(p20)

代码如下:

 import random

 def Darts(n):
k=0
i=1
while i<=n:
x=random.uniform(0,1)
#y=random.uniform(0,1)
y=x
if(x**2+y**2<=1):
k+=1
i+=1
return 4*k/n print(Darts(10000000))
print(Darts(100000000))
print(Darts(100000000))

结果如下:

物理意义:计算2*sqrt(2)  #如果结果输出的是2*k/n,则计算的是无理数sqrt(2)的近似值

ex2(p23)

代码如下:

 import random
import math def F(x):
return math.sqrt(1-x**2) def CalPI(n):
k=0
i=1
while i<=n:
i+=1
x=random.uniform(0,1)
y=random.uniform(0,1)
if (y<=F(x)):
k+=1
return 4*k/n print("when n=10000000,PI=%.10f"%CalPI(10000000))
print("when n=100000000,PI=%.10f"%CalPI(100000000))
print("when n=1000000000,PI=%.10f"%CalPI(1000000000))

结果如下:

ex3(p23)

代码如下:

 import random
import math def F(x):
return x-1 def CalCalculus(a,b,c,d,n,function):
k_positive=0
k_negtive=0
i=1
while i<=n:
i+=1
x=random.uniform(a,b)
y=random.uniform(c,d)
if (y>=0 and y<=function(x)):
k_positive+=1
elif(y<0 and y>function(x)):
k_negtive+=1
return (b-a)*(d-c)*(k_positive-k_negtive)/n if __name__=="__main__":
function=F
str=input("please input a,b,c,d:");
ceof=list(str.split(" "))
ceof=[int(i) for i in ceof]
print(ceof)
print("when n=1000000,res=%.10f"%CalCalculus(ceof[0],ceof[1],ceof[2],ceof[3],1000000,function))
print("when n=10000000,res=%.10f"%CalCalculus(ceof[0],ceof[1],ceof[2],ceof[3],10000000,function))
print("when n=100000000,res=%.10f"%CalCalculus(ceof[0],ceof[1],ceof[2],ceof[3],100000000,function))

结果如下:

p24 Ex4

ex4(p36)

代码如下:

 # -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'jing'
__mtime__ = '2017/9/20'
# code is far away from bugs with the god animal protecting
I love animals. They taste delicious.
┏┓ ┏┓
┏┛┻━━━┛┻┓
┃ ☃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━┓ ┏━┛
┃ ┗━━━┓
┃ 神兽保佑 ┣┓
┃ 永无BUG! ┏┛
┗┓┓┏━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
"""
import random
import math def CalSetCount(setN):
setTemp=set()
k=0
a=random.choice(setN)
while a not in setTemp:
k+=1
setTemp.add(a)
a = random.choice(setN)
return k if __name__=="__main__":
n=int(input("please enter n(the numbers of set):"))
while n!=0:
setN=range(0,n)
i=0
kList=[]
while i<1000:
i+=1
kList.append(CalSetCount(setN))
print("The estimated value of n is %.f"%(2.0*((sum(kList)/1000)**2)/math.pi))
n = int(input("please enter n(the numbers of set):"))

结果如下:

随着n值的增大,误差存在着波动性,但整体趋势是越来越小的

p54

p64 ex

 import random

 count=1

 def Search(val,ptr,x,i):
global count
count=1
while x>val[i]:
i=ptr[i]
count=count+1
return i def A(val,ptr,x,head):
return Search(val,ptr,x,head) def B(val,ptr,x,head):
i=head
max=val[i]
for j in range(4):
y=val[j]
if max<y and y<=x:
i=j
max=y
return Search(val,ptr,x,i) def C(val,ptr,x,head):
i=head
max=val[i]
for k in range(4):
j=random.randint(0,15)
y=val[j]
if max<y and y<=x:
i=j
max=y
return Search(val,ptr,x,i) def D(val,ptr,x,head):
i=random.randint(0,15)
y=val[i]
if x<y:
return Search(val,ptr,x,head)
elif x>y:
return Search(val,ptr,x,ptr[i])
else:
return i val=[5,7,3,0,4,11,17,14,9,20,21,25,23,30,34,31]
ptr=[1,8,4,2,0,7,9,6,5,10,12,13,11,15,-1,14] #the maxnum's index equats to -1 head=3
x=11
print("C:x=11's position is %d.Compared %d times\n"%(C(val,ptr,x,head),4+count))
print("A:x=11's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count))
print("B:x=11's position is %d.Compared %d times\n"%(B(val,ptr,x,head),4+count))
print("D:x=11's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count)) x=30
print("C:x=30's position is %d.Compared %d times\n"%(C(val,ptr,x,head),4+count))
print("A:x=30's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count))
print("B:x=30's position is %d.Compared %d times\n"%(B(val,ptr,x,head),4+count))
print("D:x=30's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count))

算法设计与分析-HomeWork的更多相关文章

  1. 【技术文档】《算法设计与分析导论》R.C.T.Lee等·第7章 动态规划

    由于种种原因(看这一章间隔的时间太长,弄不清动态规划.分治.递归是什么关系),导致这章内容看了三遍才基本看懂动态规划是什么.动态规划适合解决可分阶段的组合优化问题,但它又不同于贪心算法,动态规划所解决 ...

  2. 算法设计与分析 - AC 题目 - 第 5 弹(重复第 2 弹)

    PTA-算法设计与分析-AC原题 - 最大子列和问题 (20分) 给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+, ..., Nj },其中 ≤i ...

  3. 算法设计与分析 - AC 题目 - 第 2 弹

    PTA-算法设计与分析-AC原题7-1 最大子列和问题 (20分)给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 ...

  4. 算法设计与分析-Week12

    题目描述 You are given coins of different denominations and a total amount of money amount. Write a func ...

  5. 南大算法设计与分析课程复习笔记(1) L1 - Model of computation

    一.计算模型 1.1 定义: 我们在思考和处理算法的时候是机器无关.实现语言无关的.所有的算法运行在一种“抽象的机器”之上,这就是计算模型. 1.2 种类 图灵机是最有名的计算模型,本课使用更简单更合 ...

  6. 算法设计与分析基础 (Anany Levitin 著)

    第1章 绪论 1.1 什么是算法 1.2 算法问题求解基础 1.2.1 理解问题 1.2.2 了解计算设备的性能 1.2.3 在精确解法和近似解法之间做出选择 1.2.4 算法的设计技术 1.2.5 ...

  7. 算法设计与分析(李春保)练习题答案v1

    1.1第1 章─概论 1.1.1练习题 1.下列关于算法的说法中正确的有(). Ⅰ.求解某一类问题的算法是唯一的 Ⅱ.算法必须在有限步操作之后停止 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模 ...

  8. 算法设计与分析 - 李春葆 - 第二版 - pdf->word v3

    1.1 第1章─概论 练习题 . 下列关于算法的说法中正确的有( ). Ⅰ.求解某一类问题的算法是唯一的 Ⅱ.算法必须在有限步操作之后停止 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模糊 Ⅳ. ...

  9. 算法设计与分析 - 李春葆 - 第二版 - html v2

    1 .1 第 1 章─概论   1.1.1 练习题   1 . 下列关于算法的说法中正确的有( ).   Ⅰ Ⅱ Ⅲ Ⅳ .求解某一类问题的算法是唯一的   .算法必须在有限步操作之后停止   .算法 ...

随机推荐

  1. BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ...

  2. 基于 bi-LSTM和CRF的中文命名实体识别

    follow: https://github.com/zjy-ucas/ChineseNER  这里边主要识别的实体如图所示,其实也就主要识别人名PER,机构ORG和地点LOC: B表示开始的字节,I ...

  3. 每天一个Linux命令(2):ls命令

    版权声明 更新:2017-04-26博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下命令ls. 2 开 ...

  4. MySQL 数据底部出现总计字样 第二种办法 纵向合并 20161103

    上次在博客http://www.cnblogs.com/Mr-Cxy/p/5923375.html 我们使用了group by with rollup 函数 field自定义排序 来实现添加底部总计字 ...

  5. bzoj 3232 圈地游戏——0/1分数规划(或网络流)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3232 当然是0/1分数规划.但加的东西和减的东西不在一起,怎么办? 考虑把它们合在一起.因为 ...

  6. python+ mysql存储二进制流的方式

    很多时候我们为了管理方便会把依稀很小的图片存入数据库,有人可能会想这样会不会对数据库造成很大的压力,其实大家可以不用担心,因为我说过了,是存储一些很小的图片,几K的,没有问题的! 再者,在这里我们是想 ...

  7. 国标28181sip开源库介绍(陆续补充完备)

    (1)osip一个基于 osip 库的 UAC 和 UAS 的代码整理http://blog.csdn.net/aflyeaglenku/article/details/51601270(2)pjsi ...

  8. maven学习八 关于maven的version

      在一个有继承关系的POM文件中,父项目中有如下定义: <dependencyManagement> <dependency> <groupId>com.type ...

  9. javascript的三个函数

    作为刚刚学习javascript的小白,最近阅读了额<Javascript Dom编程艺术>,其中有三个函数感觉很是有用,特此收藏. insertAfter函数:针对insertBefor ...

  10. linux下使用c判断文件夹是否为空的小程序

    自己写了一个 判断文件夹是否为空的小代码 //文件夹操作相关的函数的帮助$: man 3 readdir #include <stdio.h> #include <sys/types ...