12. 求简单交错序列前N项和

时间:2021-12-27 03:32:54

求简单交错序列前N项和

#include <stdio.h>

int main()
{
    int denominator, flag, i, n;
    double item, sum;
    while (scanf("%d", &n) != EOF)
    {
        flag = 1;
        denominator = 1;
        sum = 0;
        for (i = 1; i <= n; i++)
        {
            item = flag*1.0/denominator;
            sum = sum+item;
            flag = -flag;
            denominator = denominator+3;
        }
        printf("sum = %.3f\n", sum);
    }
    return 0;
}