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

HDU 2139 Calculate the formula

时间:2018-09-21 15:11:32      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:bit   case   turn   space   alc   bottom   nbsp   pre   problem   

http://acm.hdu.edu.cn/showproblem.php?pid=2139

 

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 <bits/stdc++.h>
using namespace std;

const int maxn = 2345;
long long s[maxn];

int main() {
    int N;
    s[1] = 1;
    for(int i = 3; i <= maxn; i += 2)
        s[i] = s[i - 2] + i * i;

    while(~scanf("%d", &N)) {
        printf("%lld\n", s[N]);
    }

    return 0;
}

  

HDU 2139 Calculate the formula

标签:bit   case   turn   space   alc   bottom   nbsp   pre   problem   

原文地址:https://www.cnblogs.com/zlrrrr/p/9686151.html

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