[PTA] 7-34 sequence prior to the evaluation points N 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 format:
input line in a given positive integer N.

Output format:
output section and a value 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()
{
	double n,i,t;
	double sum=0,fz=2,fm=1;
	scanf("%lf",&n);
    for(i=0;i<n;i++){
        sum=sum+fz/fm;
        t=fz;
        fz=fz+fm;
        fm=t;
    }
	printf("%.2lf",sum);
	return 0;
}
Published 48 original articles · won praise 0 · Views 308

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105390132
Recommended