码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2139 Calculate the formula【水题】

时间:2015-01-29 22:36:22      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

Calculate the formula

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7441    Accepted Submission(s): 2284

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【水题】

标签:

原文地址:http://blog.csdn.net/lianai911/article/details/43281295

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!