算法设计与分析-HomeWork
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的更多相关文章
- 【技术文档】《算法设计与分析导论》R.C.T.Lee等·第7章 动态规划
由于种种原因(看这一章间隔的时间太长,弄不清动态规划.分治.递归是什么关系),导致这章内容看了三遍才基本看懂动态规划是什么.动态规划适合解决可分阶段的组合优化问题,但它又不同于贪心算法,动态规划所解决 ...
- 算法设计与分析 - AC 题目 - 第 5 弹(重复第 2 弹)
PTA-算法设计与分析-AC原题 - 最大子列和问题 (20分) 给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+, ..., Nj },其中 ≤i ...
- 算法设计与分析 - AC 题目 - 第 2 弹
PTA-算法设计与分析-AC原题7-1 最大子列和问题 (20分)给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 ...
- 算法设计与分析-Week12
题目描述 You are given coins of different denominations and a total amount of money amount. Write a func ...
- 南大算法设计与分析课程复习笔记(1) L1 - Model of computation
一.计算模型 1.1 定义: 我们在思考和处理算法的时候是机器无关.实现语言无关的.所有的算法运行在一种“抽象的机器”之上,这就是计算模型. 1.2 种类 图灵机是最有名的计算模型,本课使用更简单更合 ...
- 算法设计与分析基础 (Anany Levitin 著)
第1章 绪论 1.1 什么是算法 1.2 算法问题求解基础 1.2.1 理解问题 1.2.2 了解计算设备的性能 1.2.3 在精确解法和近似解法之间做出选择 1.2.4 算法的设计技术 1.2.5 ...
- 算法设计与分析(李春保)练习题答案v1
1.1第1 章─概论 1.1.1练习题 1.下列关于算法的说法中正确的有(). Ⅰ.求解某一类问题的算法是唯一的 Ⅱ.算法必须在有限步操作之后停止 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模 ...
- 算法设计与分析 - 李春葆 - 第二版 - pdf->word v3
1.1 第1章─概论 练习题 . 下列关于算法的说法中正确的有( ). Ⅰ.求解某一类问题的算法是唯一的 Ⅱ.算法必须在有限步操作之后停止 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模糊 Ⅳ. ...
- 算法设计与分析 - 李春葆 - 第二版 - html v2
1 .1 第 1 章─概论 1.1.1 练习题 1 . 下列关于算法的说法中正确的有( ). Ⅰ Ⅱ Ⅲ Ⅳ .求解某一类问题的算法是唯一的 .算法必须在有限步操作之后停止 .算法 ...
随机推荐
- Maven项目中使用JUnit进行单元测试
1.打开maven项目中的pom.xml,添加JUnit 的jar包 2.在src/test/java下右键新建JUnit Test Cast
- HDU 6170 Two strings (dp)
/** * 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170 * 字符串match, '.'代表匹配任意一个字符,"*" 代表 ...
- linux 进程学习笔记-暂停进程
<!--[if !supportLists]-->Ÿ <!--[endif]-->暂停进程 int pause() 其会挂起当前进程直到有信号来唤醒或者进程被结束. 随便提一下 ...
- UNR #1 火车管理
很简单 用一个线段树维护 1.答案 2.当前栈顶是什么时候push进来的 然后用一棵以时间为版本的可持久化线段树维护每个操作之后第一个覆盖到他的操作是哪个 就可以了 询问直接在线段树上询问,修改在两棵 ...
- ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)
Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...
- 【Lintcode】103.Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- configured to save RDB snapshots, but is currently not able to persist o...
Redis问题 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d ...
- dataguard 下主备 online redo 与 standby redo log resize 重建
环境说明: 本实验环境是一个节点的rac + 单节点 asm dg database 与 grid 版本是 11.2.0.4 .提别提醒 如果是多节点集群,操作时需要特别注意 thread . ...
- AtCoder Grand Contest 014 D:Black and White Tree
题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_d 题目翻译 给你一棵树,每次任选一个点染色,先手染白色,后手染黑色.如果最后存在一个白色的点 ...
- docker添加阿里云镜像加速器
.docker添加阿里云镜像加速器 https://blog.csdn.net/chenjin_chenjin/article/details/86674521 .配置阿里云加速器 阿里云会根据账号生 ...