Problem Description
You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2.
Input
In each case, there is an odd positive integer n.
Output
Print the sum. Make sure the sum will not exceed 2^31-1
Sample Input
3
Sample Output
10#include<iostream> #include<cstdio> using namespace std; int main() { // 公式 1^2+3^2+5^2+...+(2n-1)^2 ==n*(n+1)*(n+2)/6 unsigned __int64 n,s; while(scanf("%I64u",&n)!=EOF&&n!=0) { s=n*(n+1)*(n+2)/6; printf("%I64u\n",s); } return 0; }
奇数平方和计算
本文介绍了一种快速计算奇数序列平方和的方法,并提供了一个C++实现示例。该算法利用数学公式1^2+3^2+5^2+...+(2n-1)^2=n*(n+1)*(n+2)/6进行高效计算。
3637

被折叠的 条评论
为什么被折叠?



