正确代码
#include <stdio.h>
#include <math.h>
int main(void) {
int flag = 1, i;
double eps, item, sum, denominator = 1;
sum = 0;
for (i = 1; i <= 100; i++) {
item = flag / denominator ;
sum = sum + item;
denominator = denominator + 3;
printf("%f", item);
getchar();
}
return 0;
}
注意 denominator = 1;的变量确定类型!
错误代码
#include <stdio.h>
#include <math.h>
int main(void) {
int flag = 1, i, denominator = 1;
double eps, item, sum;
sum = 0;
for (i = 1; i <= 100; i++) {
item = flag / denominator ;
sum = sum + item;
denominator = denominator + 3;
printf("%f", item);
getchar();
}
return 0;
}在转换变量类型后 结果会一直为0,是一个非常容易漏掉并且极难发现的错误,在此记录