Problem 4-3 N sequence prior to the evaluation score and (15 minutes)

This problem requires programming, calculates the sequence 2/3 + 1/2 + 5/8 + 3/5 + ... and the sum of the first N. Note that this sequence from the item 2, for each one molecule of the former one and the numerator and denominator, the denominator is a molecular precursor.

Input formats:

In a given input row positive integer N.

Output formats:

Value output portion and in a row, accurate to two decimal places. Title ensure that the calculation result does not exceed the double precision.

Sample input:

20
 

Sample output:

32.66


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

 

Guess you like

Origin www.cnblogs.com/Kimsohyun/p/12578900.html