码迷,mamicode.com
首页 > 其他好文 > 详细

有进度条的圆周率

时间:2020-03-30 21:32:11      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:scale   inf   print   图片   its   hits   --   mic   src   

from random import random
import time
import math
print("------Begin------")
start = time.perf_counter()
scale = 10
for i in range(scale+1):
    a = **i
    b = .*(scale-i)
    c = (i/scale)*100
    print("{:^3.0f}%[{}->{}]".format(c,a,b))
    time.sleep(0.5) 
DARTS = 1000*1000
hits = 0.0
start = time.perf_counter()
for i in range(1,DARTS+1):
    x,y = random(),random()
    dist = pow(x**2+y**2,0.5)
    if dist<=1.0:
        hits += 1
pi = 4*(hits/DARTS)
print("圆周率为:{}".format(pi))
end=time.perf_counter()
print("运行时间:{:.2f}s".format(end-start))     
print("------End------")

技术图片

有进度条的圆周率

标签:scale   inf   print   图片   its   hits   --   mic   src   

原文地址:https://www.cnblogs.com/ssh-4016/p/12601089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!