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------")
原文地址:https://www.cnblogs.com/ssh-4016/p/12601089.html
时间: 2024-10-12 17:07:54