算法设计与分析-HomeWork
ex1(p20)
代码如下:
1 import random 2 3 def Darts(n): 4 k=0 5 i=1 6 while i<=n: 7 x=random.uniform(0,1) 8 #y=random.uniform(0,1) 9 y=x 10 if(x**2+y**2<=1): 11 k+=1 12 i+=1 13 return 4*k/n 14 15 print(Darts(10000000)) 16 print(Darts(100000000)) 17 print(Darts(100000000))
结果如下:
物理意义:计算2*sqrt(2) #如果结果输出的是2*k/n,则计算的是无理数sqrt(2)的近似值
ex2(p23)
代码如下:
1 import random 2 import math 3 4 def F(x): 5 return math.sqrt(1-x**2) 6 7 def CalPI(n): 8 k=0 9 i=1 10 while i<=n: 11 i+=1 12 x=random.uniform(0,1) 13 y=random.uniform(0,1) 14 if (y<=F(x)): 15 k+=1 16 return 4*k/n 17 18 print("when n=10000000,PI=%.10f"%CalPI(10000000)) 19 print("when n=100000000,PI=%.10f"%CalPI(100000000)) 20 print("when n=1000000000,PI=%.10f"%CalPI(1000000000))
结果如下:
ex3(p23)
代码如下:
1 import random 2 import math 3 4 def F(x): 5 return x-1 6 7 def CalCalculus(a,b,c,d,n,function): 8 k_positive=0 9 k_negtive=0 10 i=1 11 while i<=n: 12 i+=1 13 x=random.uniform(a,b) 14 y=random.uniform(c,d) 15 if (y>=0 and y<=function(x)): 16 k_positive+=1 17 elif(y<0 and y>function(x)): 18 k_negtive+=1 19 return (b-a)*(d-c)*(k_positive-k_negtive)/n 20 21 if __name__=="__main__": 22 function=F 23 str=input("please input a,b,c,d:"); 24 ceof=list(str.split(" ")) 25 ceof=[int(i) for i in ceof] 26 print(ceof) 27 print("when n=1000000,res=%.10f"%CalCalculus(ceof[0],ceof[1],ceof[2],ceof[3],1000000,function)) 28 print("when n=10000000,res=%.10f"%CalCalculus(ceof[0],ceof[1],ceof[2],ceof[3],10000000,function)) 29 print("when n=100000000,res=%.10f"%CalCalculus(ceof[0],ceof[1],ceof[2],ceof[3],100000000,function))
结果如下:
p24 Ex4
ex4(p36)
代码如下:
1 # -*- coding: utf-8 -*- 2 """ 3 __title__ = '' 4 __author__ = 'jing' 5 __mtime__ = '2017/9/20' 6 # code is far away from bugs with the god animal protecting 7 I love animals. They taste delicious. 8 ┏┓ ┏┓ 9 ┏┛┻━━━┛┻┓ 10 ┃ ☃ ┃ 11 ┃ ┳┛ ┗┳ ┃ 12 ┃ ┻ ┃ 13 ┗━┓ ┏━┛ 14 ┃ ┗━━━┓ 15 ┃ 神兽保佑 ┣┓ 16 ┃ 永无BUG! ┏┛ 17 ┗┓┓┏━┳┓┏┛ 18 ┃┫┫ ┃┫┫ 19 ┗┻┛ ┗┻┛ 20 """ 21 import random 22 import math 23 24 def CalSetCount(setN): 25 setTemp=set() 26 k=0 27 a=random.choice(setN) 28 while a not in setTemp: 29 k+=1 30 setTemp.add(a) 31 a = random.choice(setN) 32 return k 33 34 if __name__=="__main__": 35 n=int(input("please enter n(the numbers of set):")) 36 while n!=0: 37 setN=range(0,n) 38 i=0 39 kList=[] 40 while i<1000: 41 i+=1 42 kList.append(CalSetCount(setN)) 43 print("The estimated value of n is %.f"%(2.0*((sum(kList)/1000)**2)/math.pi)) 44 n = int(input("please enter n(the numbers of set):"))
结果如下:
随着n值的增大,误差存在着波动性,但整体趋势是越来越小的
p54
p64 ex
1 import random 2 3 count=1 4 5 def Search(val,ptr,x,i): 6 global count 7 count=1 8 while x>val[i]: 9 i=ptr[i] 10 count=count+1 11 return i 12 13 def A(val,ptr,x,head): 14 return Search(val,ptr,x,head) 15 16 def B(val,ptr,x,head): 17 i=head 18 max=val[i] 19 for j in range(4): 20 y=val[j] 21 if max<y and y<=x: 22 i=j 23 max=y 24 return Search(val,ptr,x,i) 25 26 def C(val,ptr,x,head): 27 i=head 28 max=val[i] 29 for k in range(4): 30 j=random.randint(0,15) 31 y=val[j] 32 if max<y and y<=x: 33 i=j 34 max=y 35 return Search(val,ptr,x,i) 36 37 def D(val,ptr,x,head): 38 i=random.randint(0,15) 39 y=val[i] 40 if x<y: 41 return Search(val,ptr,x,head) 42 elif x>y: 43 return Search(val,ptr,x,ptr[i]) 44 else: 45 return i 46 47 48 49 val=[5,7,3,0,4,11,17,14,9,20,21,25,23,30,34,31] 50 ptr=[1,8,4,2,0,7,9,6,5,10,12,13,11,15,-1,14] #the maxnum's index equats to -1 51 52 head=3 53 x=11 54 print("C:x=11's position is %d.Compared %d times\n"%(C(val,ptr,x,head),4+count)) 55 print("A:x=11's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count)) 56 print("B:x=11's position is %d.Compared %d times\n"%(B(val,ptr,x,head),4+count)) 57 print("D:x=11's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count)) 58 59 x=30 60 print("C:x=30's position is %d.Compared %d times\n"%(C(val,ptr,x,head),4+count)) 61 print("A:x=30's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count)) 62 print("B:x=30's position is %d.Compared %d times\n"%(B(val,ptr,x,head),4+count)) 63 print("D:x=30's position is %d.Compared %d times\n"%(A(val,ptr,x,head),count))