PTA
7-50 近似求PI
网友代码:
include <stdio.h> int main(){
double eps, sum=, i, temp=;
scanf("%le", &eps);
for(i=; temp>eps; i++){
temp = temp*i/(*i+);
sum += temp;
}
printf("PI = %.5f\n", *sum); return ;
}
我的代码:
include<stdio.h>
int main() {
int fz=,fm=,k=;
double t=1.0,pi=1.0,eps;
scanf("%le",&eps);
while(t>=eps) {
fz*=k;
fm*=(*k+);
t = (double)fz/fm;
pi += t;
k++;
}
printf("%lf",*pi);
}
数据大了会报错,k会超出int范围
几点不足:
1、变量类型不一致容易出错
2、变量太多程序不易读