A23643. 金字塔金字塔由n 层石块垒成。从塔底向上,每层依次需要 n×n,(n-1)×(n-1),...,2×2,1×1块石块。请问搭建金字塔总共需要多少块石块?
填空题
容易
知识点
题目描述
金字塔
金字塔由n 层石块垒成。从塔底向上,每层依次需要 n×n,(n-1)×(n-1),...,2×2,1×1块石块。请问搭建金字塔总共需要多少块石块?
输入格式
一行,一个正整数n ,表示金字塔的层数。
输出格式
一行,一个正整数,表示搭建金字塔所需的石块数量。
样例
输入样例 1
2输出样例 1
5输入样例 2
5输出样例 2
55数据范围
对于所有测试点,保证1≤n≤50 。
参考答案
#include <algorithm>
#include <cstdio>
using namespace std;
int n, ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) ans += i * i;
printf("%d\n", ans);
return 0;
}
上一题
下一题