Write a program, input a positive integer n and calculate the sum of the first n terms of 1-2/3+3/5-4/7+5/9-6/11+…
#include<stdio.h>
int main()
{
int n,i,deno;
double sum,a;
scanf("%d",&n);
a=1;
sum=0;deno=1;
for(i=1;i<=n;i++)
{
sum=a*(1.0*i/deno)+sum;
deno+=2;
a=-a;
}
printf("%.3f\n",sum);
return 0;
}
此篇博客介绍了如何使用C++编写程序,通过迭代计算1-2/3+3/5-4/7+...的前n项之和。程序利用for循环和交替符号技巧,展示了如何处理带有正负分数的级数求和问题。
4183

被折叠的 条评论
为什么被折叠?



