2008年 07月 20日 的归档
google code jam
玩了下google编程大赛,我那轮3道题目,题目球猫帖这里了,其中的第3,超烦,算什么打苍蝇的概率,算的累死了,为了不让自己的程序白写,贴在这里晒晒,嘿嘿.
PS: 初学python,发现真好用…
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- import math
- def sg(x,R):
- '''求弓形面积'''
- return math.acos(x/R)*(R**2)-x*math.sqrt(R**2-x**2)
- inf=file('C-large.in','r')
- outf=file('C-large.out','w')
- casenum=int(inf.readline())
- for c in range(0,casenum):
- tmp=inf.readline()
- f=float(tmp.split(' ')[0])
- R=float(tmp.split(' ')[1])
- t=float(tmp.split(' ')[2])
- r=float(tmp.split(' ')[3])
- g=float(tmp.split(' ')[4])
- TotalArea=math.pi*R**2/4.0
- A=0
- fn=0
- r=r+f
- t=t+f
- g=g-2*f
- if g > 0:
- i=0
- while i*(g+r*2.0)+r < (R-t):
- x=i*(g+r*2.0)+r
- xp=x+g
- j=0
- Yi=math.sqrt((R-t)**2-x**2)
- try:
- Yip=math.sqrt((R-t)**2-xp**2)
- except:
- Yip=0
- while j*(g+r*2.0)+r 0:
- fn+=nfn
- j+=nfn
- #print "c=",c,"i=",i,"j=",j,nfn
- continue
- if xp < R-t:
- if yp Yip and yp <= Yi:
- if y Yi
- if y < Yip:
- A+=(sg(x,R-t)-sg(xp,R-t))/2.0-g*y
- #w="3a.不规则"
- elif y < Yi:
- A+=(sg(y,R-t)-sg(Yi,R-t))/2.0-(Yi-y)*x
- #w="3b.不规则"
- else: #这个是不可能的
- print "c=",c,"i=",i,"j=",j,"y=",y,"yp=",yp,"Yi=",Yi,"Yip=",Yip,"x=",x,"xp=",xp,"R-t=",R-t #,w
- else:
- if yp <= Yi:
- A+=(sg(y,R-t)-sg(yp,R-t))/2.0-g*x
- #w="4.下不规则"
- else:
- A+=(sg(y,R-t)-sg(Yi,R-t))/2.0-(Yi-y)*x
- #w="5.三角不规则"
- j+=1
- i+=1
- A+=fn*g*g
- #print TotalArea,A
- outf.write('Case #%d: %0.6f\n'%(c+1,(1-A/TotalArea)))
- inf.close()
- outf.close()