Exercise 4-3 Find the sum of the first N items in the score sequence (15 points)

Exercise 4-3 Find the sum of the first N items in the sequence of scores (15 points)
This problem requires writing a program to calculate the sum of the first N items in the sequence 2/1+3/2+5/3+8/5+... Note that in this sequence from the second term, the numerator of each term is the sum of the numerator and denominator of the previous term, and the denominator is the numerator of the previous term.

Input format:
Input a positive integer N in one line.

Output format:
Output the partial sum value in one line, accurate to two decimal places. The title guarantees that the calculation result does not exceed the double precision range.

Input sample:
20
Output sample:
32.66

#include<stdio.h>
int main()
{
    
    
    int n,i;
    double a=2,b=1,sum=0,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    
    
        sum+=a/b;
        t=a;
        a=a+b;
        b=t;
    }
    printf("%.2lf",sum);
    return 0;
}

Guess you like

Origin blog.csdn.net/ChaoYue_miku/article/details/115094041