当前位置:网站首页>2.4 finding the sum of the first n terms of the interleaved sequence

2.4 finding the sum of the first n terms of the interleaved sequence

2022-06-25 19:57:00 Then peace and happiness

This question requires the preparation of procedures , Calculate interleaved sequence 1-2/3+3/5-4/7+5/9-6/11+… Before N Sum of items .‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

Input format :

Input gives a positive integer on a line N.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

Output format :

Output the value of the partial sum in one line , The result is three decimal places .‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

sample input :

5

sample output :

0.917

Reference code :

#include<stdio.h>
int main(void)
{
    
	int i, flag, b, d, n;
	double sum;
	scanf("%d", &n);
	sum=0; flag=1;d=1;b=1;
	for(i=1; i<=n; i++)
	{
    
		sum=sum+b*flag*1.0/d;
		flag=flag+1;
		b=-b;
		d=d+2;
	}
	printf("%.3f", sum);
	return 0;
}
原网站

版权声明
本文为[Then peace and happiness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190512046212.html