I am LAZY bones? AN ancient AND boring SITE

2008年 07月 20日 的归档

google code jam

玩了下google编程大赛,我那轮3道题目,题目球猫帖这里了,其中的第3,超烦,算什么打苍蝇的概率,算的累死了,为了不让自己的程序白写,贴在这里晒晒,嘿嘿.
PS: 初学python,发现真好用…

  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. import math
  4.  
  5. def sg(x,R):
  6.     '''求弓形面积'''
  7.     return math.acos(x/R)*(R**2)-x*math.sqrt(R**2-x**2)
  8.  
  9. inf=file('C-large.in','r')
  10. outf=file('C-large.out','w')
  11. casenum=int(inf.readline())
  12. for c in range(0,casenum):
  13.     tmp=inf.readline()
  14.     f=float(tmp.split(' ')[0])
  15.     R=float(tmp.split(' ')[1])
  16.     t=float(tmp.split(' ')[2])
  17.     r=float(tmp.split(' ')[3])
  18.     g=float(tmp.split(' ')[4])
  19.  
  20.     TotalArea=math.pi*R**2/4.0
  21.     A=0
  22.     fn=0
  23.     r=r+f
  24.     t=t+f
  25.     g=g-2*f
  26.     if g > 0:
  27.         i=0
  28.         while i*(g+r*2.0)+r < (R-t):
  29.             x=i*(g+r*2.0)+r
  30.             xp=x+g
  31.             j=0
  32.             Yi=math.sqrt((R-t)**2-x**2)
  33.             try:
  34.                 Yip=math.sqrt((R-t)**2-xp**2)
  35.             except:
  36.                 Yip=0
  37.             while j*(g+r*2.0)+r  0:
  38.                     fn+=nfn
  39.                     j+=nfn
  40.                     #print "c=",c,"i=",i,"j=",j,nfn
  41.                     continue
  42.  
  43.                 if xp < R-t:
  44.                     if yp  Yip and yp <= Yi:
  45.                         if y  Yi
  46.                         if y < Yip:
  47.                             A+=(sg(x,R-t)-sg(xp,R-t))/2.0-g*y
  48.                             #w="3a.不规则"
  49.                         elif y < Yi:
  50.                             A+=(sg(y,R-t)-sg(Yi,R-t))/2.0-(Yi-y)*x
  51.                             #w="3b.不规则"
  52.                         else: #这个是不可能的
  53.                             print "c=",c,"i=",i,"j=",j,"y=",y,"yp=",yp,"Yi=",Yi,"Yip=",Yip,"x=",x,"xp=",xp,"R-t=",R-t #,w
  54.                 else:
  55.                     if yp <= Yi:
  56.                         A+=(sg(y,R-t)-sg(yp,R-t))/2.0-g*x
  57.                         #w="4.下不规则"
  58.                     else:
  59.                         A+=(sg(y,R-t)-sg(Yi,R-t))/2.0-(Yi-y)*x
  60.                         #w="5.三角不规则"
  61.  
  62.                 j+=1
  63.             i+=1
  64.     A+=fn*g*g
  65.     #print TotalArea,A
  66.     outf.write('Case #%d: %0.6f\n'%(c+1,(1-A/TotalArea)))
  67. inf.close()
  68. outf.close()