[HDU1001] Sum Problem
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
1
100
Sample Output
1
5050
分析
输入n,求1+2+...+n的和。
方法有两种:
1. 直接求法
使用一个for循环进行累加。用s表示总和,s初始化为0,然后再维护一个循环变量i。代码:
int s = ;
for (int i = ; i <= n; i++)
s += i;
printf("%d\n\n", s);
完整C程序:
#include <stdio.h>
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int s = ;
for (int i = ; i <= n; i++)
s += i;
printf("%d\n\n", s);
}
return ;
}
2. 公式法
还记得高斯吧,他小时候就计算出了1+2+...+100=5050。方法1就像是其他同学,方法2则是高斯。
进入正题,等差数列有一个公式:总和=(首项+末项)*项数/2。这里首项=1,末项=n,项数=n,因此1+2+...+n=(1+n)*n/2。代码:
printf("%lld\n\n", (long long)( + n) * (long long)n / 2LL);
但有一个类型问题需要注意:description中说总和是不超过32位有符号整数的范围的(也就是2^31-1或2147483647),这说明(1+n)*n/2<=2147483647,但不代表(1+n)*n也是小于2147483647的。事实上,当n>=14654时,(1+n)*n就超过2147483647了。这种情况下,进行运算将会出现溢出错误。因此需要将1+n和n转换成long long(其实只要转1个就可以了,后面的2LL也可以直接写2)。当然,算出(1+n)*n后将其转成int再用%d打印也没有问题。(P.S. 我就是因为这个原因WA的……)
完整C程序:
#include <stdio.h>
int main()
{
int n;
while (scanf("%d", &n) != EOF)
printf("%lld\n\n", (long long)n * (long long)(n + ) / 2LL);
return ;
}
注意
每次输出要输出两个换行符!
[HDU1001] Sum Problem的更多相关文章
- summary of k Sum problem and solutions in leetcode
I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏
Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HD2058The sum problem
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Maxmum subsequence sum problem
We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- NYOJ--927--dfs--The partial sum problem
/* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...
- 动态规划法(三)子集和问题(Subset sum problem)
继续讲故事~~ 上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界. 这天,丁丁刚回到家,他 ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- CocoaAsyncSocket + Protobuf 处理粘包和拆包问题
在上一篇文章<iOS之ProtocolBuffer搭建和示例demo>分享环境的搭建, 我们和服务器进行IM通讯用了github有名的框架CocoaAsynSocket, 然后和服务器之间 ...
- 卷积神经网络CNN与深度学习常用框架的介绍与使用
一.神经网络为什么比传统的分类器好 1.传统的分类器有 LR(逻辑斯特回归) 或者 linear SVM ,多用来做线性分割,假如所有的样本可以看做一个个点,如下图,有蓝色的点和绿色的点,传统的分类器 ...
- node.js系列(模块):request模块实现与php的通讯
app.js: var express = require('express'); var request = require('request'); var app = express(); /*r ...
- HDFS中NameNode启动过程
移动到hadoop文件目录下 NameNode启动命令:sbin/hadoop-daemon.sh start namenode DataNode启动命令:sbin/hadoop-daemon.sh ...
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- DirectFB学习笔记二
本篇目的,画一个方框,在方框上画一串字符. 实现步骤:首先创建IDirectFB接口,通过它再创建要显示的表面surface,同时创建字体font,绘制字符必须要设置绘制的字体,否则绘制不成功.然后清 ...
- JS模式--装饰者模式(用AOP动态改变函数的参数)
Function.prototype.before = function (beforefn) { var _self = this; return function () { beforefn.ap ...
- 我拖拖拖--H5拖放API基础篇
不要搞错,本文不是讲如何拖地的.看过<javascript精粹>朋友应该知道,他实现拖放的过程比较复杂,现在时代不同了,我们用H5的新的拖放API就能非常方便的实现拖放效果了.最近在园子见 ...
- poj2739尺取法+素数筛
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...
- mysql的下载地址+Download WinMD5
http://dev.mysql.com/downloads/mysql http://www.nullriver.com/products