当前位置:网站首页>2.3 partial sum of square and reciprocal sequences

2.3 partial sum of square and reciprocal sequences

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

This problem requires two positive integers m and n(m≤n) Programming , Calculate the sequence and .

Input format :

Enter two positive integers on one line m and n(m≤n), They are separated by spaces .‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

Output format :

In a row, press “sum = S” The format of the output part and the value of S, To six decimal places . The calculation results should not exceed the double precision range .‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬

sample input :

5 10

sample output :

sum = 355.845635

  Code :

#include <stdio.h>
#include <math.h>
int main ()
{
	int m,n;
	scanf("%d %d",&m,&n);
	double sum1=0.0, sum2=0.0, sum;
	int i;
	for(i=m;i<=n;i++){
		sum1=sum1+i*i;
		sum2=sum2+1.0/i;
	}
	sum=sum1+sum2;
	printf("sum = %.6f",sum);
	
	return 0;
}

原网站

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