题目:求1/1-1/2+1/3.....-1/100的和
#include<stdio.h>
int main()
{
double sum = 0.0;
int flat = 1;
double i = 0.0;
for (double i = 1.0; i <= 100; i++)
{
sum += flat * 1.0 / i;//乘1.0是将flat分数化
flat = -flat;
}
printf("sum=%lf", sum);
return 0;
}
运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/044ec562b005a5ef5b6bb453bcfef1c4.png)