计算圆周率PI
服务器地址 172.16.192.24
用户名 2019301110060
计算圆周率
#include <time.h>
#include <stdio.h>
double step;
int main(){
static long num_steps;
printf("请输入一个1000以上的数字求取PI值\n>>");
scanf("%ld",&num_steps);
clock_t start_t, end_t;
double total_t;
int i;
double x,pi,sum =0.0;
start_t = clock();
step=1.0/(double)num_steps; //将1平分成100000步
for(i=1;i<=num_steps;i++){
x=(i-0.5)*step;//获得0-1的连续值
sum+=4.0/(1.0+x*x);
}
pi =step * sum; //
end_t = clock();
total_t=(double)(end_t - start_t)/CLOCKS_PER_SEC;
printf("运行时间为%fs\t%.10f\r\n",total_t,pi);
return 0;
}加上时间和精度的控制
输出结果
Last updated
Was this helpful?