SWUSTOJ #299 平方和

版权声明:知识不设限,可自由转载,请附上连接: https://blog.csdn.net/qq_44475551/article/details/89635213

题目

用递归的方法求 f(n) = 1 * 1 + 2 * 2 + 3 * 3 +……+ n * n

输入

输入数字n

输出

输出结果 f(n)

样例输入

1
3

样例输出

1
14

源代码

#include <stdio.h>
int f(int n)
{
	if (n == 1)
	{
  		return 1;
	}
	return f(n-1)+n*n;
}

int main()
{
	int n;
	while (scanf("%d", &n) == 1)
	{
 		printf("%d\n", f(n));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44475551/article/details/89635213