Find the sum of a sequence of numbers

Insert picture description here

//求一种数列的和
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    
    
	double n,m;
	while(scanf("%lf%lf",&n,&m) !=EOF)
	{
    
    
		int i=0;
		double s=0;
		while(i<m){
    
    
			s+=n;
			n=sqrt(n);
			i+=1;
		}
		printf("%.2lf\n",s);
	}
	return 0;
} 

Debugging report:
1. At the beginning of the input and no output, immediately respond to whether you have forgotten &, as expected, forgot. .
2. At the beginning, define s as an integer type, of course it is a floating point type.
3. At the beginning, set n and m to an integer type according to the requirements of the title, but found that the output is an'integer' with two decimal places. 00, consciousness It should be the type of n in the inner while loop, so use a double precision with higher precision (it can be understood as a real number for the time being)
. 4. Even the reserved two decimal places are wrongly written as %2.lf, the basic knowledge is not strong enough! It should be %.2lf.
Someone should also think of the subscript implementation:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    
    
	int m;
	double s,n,a[10010];
	while(scanf("%lf%d",&n,&m)!=EOF){
    
    
		s=0;
		for(int i=1;i<=m;i++){
    
    
			a[i]=n;
			s=s+a[i];
			n=sqrt(n);
		}
		printf("%.2lf\n",s);
	}
	return 0;
 } 

Essentially, the value of n is added to s while changing, so as to meet the requirements of the problem
. Come on with ZDZ!
The young and strong do not work hard, the
boss is sad!
Oh yeah!

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113489794