HDU2139 Calculate the formula【水题】

2024-06-15 05:32

本文主要是介绍HDU2139 Calculate the formula【水题】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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://www.chinasem.cn/article/1062552

相关文章

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

Codeforces Round #182 (Div. 2)A(水题)

题目链接:http://codeforces.com/contest/302/problem/A 解题思路: 只要通过重新排列使区间内和为0即是1,否则是0. 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#inc

HDU 2064 汉诺塔III(水题)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=2064 题目大意: 有三根杆,求把n个圆盘从左边移到右边,最少需要移动圆盘的次数。移动规则为大盘不能放在小盘上,比原始的汉诺塔题改变的地方是,只能通过中间的杆往左右两边的杆移动。 心得: 此题心得在题外,不在题内,初看此题,尼玛吓了一跳,好像很难的样子,手贱百度了一下,只注意到俩字“水题”,赶紧

【SGU】115. Calendar 水题= =

传送门:【SGU】115. Calendar 题目分析:2001年1月1号星期1,然后就没什么好说的了= = 代码如下: #include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespac

Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)

解题报告 意思就是说有n行柜子,放奖杯和奖牌,要求每行柜子要么全是奖杯要么全是奖牌,而且奖杯每行最多5个,奖牌最多10个。 直接把奖杯奖牌各自累加,分别出5和10,向上取整和N比较 #include <iostream>#include <cstdio>#include <cstring>#include <stdlib.h>#include <algorithm>

hdu 1013(水题)

题意:求一个数,各个数位相加,如果结果小于10则输出,否者递归进行数位相加。   #include <cstdio>#include <cstring>#include <string>#include <iostream>using namespace std;int result(int n){int sum = 0;while (n > 0){sum += n % 1

HDU 1008(水题)

题意:给一个数n,后跟着n个数,代表电梯要到的层数,如果是上升,则每层花费6分钟,下降每层划分4分钟,停着话费5分钟,求电梯总共花费多少时间。   #include <iostream>using namespace std;void main(){int n, m, t, total;while (cin >> n && n != 0){m = 0;total = 0;wh

HDU 1001(水题)

题意:输入n,求1~n之和。 import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner s=new Scanner(System.in);int n=0;int sum;while(s.hasNextInt()){sum=0;n=s.nextInt();

HDU 1000 (水题)

题意:输入两个整数,求两个整数的和。   #include <cstdio>int main(){int a, b;while (scanf("%d%d", &a, &b) != EOF){printf("%d\n", a+b);}}

HDU 1128(水题)

题意:如题。 #include<stdio.h>#include<memory.h>#define N 1000001int visited[N];int Self(int n){int sum=n;while(n/10){sum+=n%10;n/=10;}sum+=n;return sum;}int main(){memset(visited,0,sizeof