求数列的和

递归解法:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int m, n;

double f(int k)
{
	if(k == 1)	return n;
	return sqrt(f(k - 1));
}

int main()
{
	while(~scanf("%d %d", &n, &m))
	{
		double sum = 0;
		for(int i = 1; i <= m; ++ i)
		{
			sum += f(i);
		}
		printf("%.2f\n", sum);
	}
	
	return 0;
}

  

循环解法:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
	int m;
	double n;
	while(~scanf("%lf %d", &n, &m))
	{
		double sum = n, a = n;
		for(int i = m; i > 1; -- i)
		{
			a = sqrt(a);
			sum += a;
		}
		printf("%.2f\n", sum);
	}
	
	return 0;
}

  

posted @ 2019-07-23 16:55  青衫客36  阅读(142)  评论(0编辑  收藏  举报