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. 【转】PHP生成器 (generator)和协程的实现

    原文地址:https://phphub.org/topics/1430 1.一切从 Iterator 和 Generator 开始 为便于新入门开发者理解,本文一半篇幅是讲述迭代器接口(Iterato ...

  2. stack_1.设计一个有getMin功能的栈

    思路 : 生成两个栈($stack ,$stack_min ),往$stack塞数据($value)的时候 ,比较一下$value和$stack_min最上面的元素的大小,如果$value小,则压入$ ...

  3. PHP实现简单爬虫-抓取网页url

    <?php /** * 爬虫程序 -- 原型 * * 从给定的url获取html内容 * * @param string $url * @return string */ function _g ...

  4. windows 查看物理内存有几条以及查看电脑系统版本号的命令(dxdiag)

  5. 1076 Forwards on Weibo (30)(30 分)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  6. 主备角色switch

    理论知识:Switchover 切换允许primary 和一个备库进行切换,并且这种切换没有数据丢失. 前提条件: 1) 主备库相关参数 fal_client.fal_server .standby_ ...

  7. Poj1007_DNA Sorting(面向对象方法)

    一.Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that a ...

  8. JS---基础用法2

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. Pointer 指针

    利用指针访问对象,指针指向一个对象,允许使用解引用符(操作符*)来访问对象 int ival = 42; int *p = &ival;//p存放变量ival的内存地址,p是指向变量ival的 ...

  10. SSM之全局异常处理器

    1. 异常处理思路 首先来看一下在springmvc中,异常处理的思路:   如上图所示,系统的dao.service.controller出现异常都通过throws Exception向上抛出,最后 ...