编程求以下级数前n项之和:
s=1-1/3+1/5-1/7+1/9-1/11+1/13-1/15+.
输入:一个正整数n
输出:前n项和的值
#include<stdio.h>
#include<math.h>
int main()
{
double sum = 0;
int n;
scanf("%d",&n);
double a = 0;
for (int i = 1;i <= n;i++)
{
if (i % 2 == 0)
{
a = a - (1 / (2 * i - 1));
sum = sum + a;
}
else
{
a = a + (1 / (2 * i - 1));
sum = sum + a;
}
}
printf("%lf", sum);
return 0;
}