ZOJ1337
这是一个利用数学定理的估计π值得题目,只要看懂了题目说什么就很简单了。题目大意为任意给出n个数,算出有几个数对(第一个数小于第二个数,例如(2,3)),然后算出这些数对中有多少是互质的,则π=sqrt(6.0*总对数/互质对数),代码如下:
<span style="font-family:KaiTi_GB2312;font-size:14px;">#-*- coding:utf-8 -*-
#2015-03-11
import math
def huzhi(x,y):
while y!=0:
x = x%y
(x,y)=(y,x)
return x
n = input()
while n!=0:
alist = []
blist = []
count = 0
while n > 0:
a = input()
alist.append(a)
n -= 1
alist.sort()
for i in range(len(alist)):
temp = []
temp.append(alist[i]) #将数对以二维列表的形式储存
for j in alist[i+1:]:
temp.append(j)
blist.append(temp) #一个数对即为一个小列表
temp = temp[:1]
for i in blist:
if huzhi(i[1], i[0])==1:
count += 1
if count == 0: #若互质对数为0,则公式不成立
print 'No estimate for this data set.'
else:
print '%.6f' % math.sqrt(6.0*len(blist)/count)
n = input()
</span>