39. 求分数序列前N项和

时间:2021-08-28 03:35:21

求分数序列前N项和

#include <stdio.h>

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