本文主要是介绍HDU2139 Calculate the formula【水题】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
Author
wangye
Source
HDU 2007-11 Programming Contest_WarmUp
题目大意:给你一个奇数N,求1~N中奇数的平方和。
思路:直接暴力超时了,所以用公式来做 S = N*(N+1)*(N+2)/6,因为结果不超int型,
但是中间过程会超一些,所以用__int64来做就可以了。注意cin、cout会超时,用scanf
和printf就可以了。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;int main()
{__int64 N,sum;while(~scanf("%I64d",&N)){sum = 0;sum = N*(N+1)*(N+2)/6;printf("%I64d\n",sum);}return 0;
}
这篇关于HDU2139 Calculate the formula【水题】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!