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的更多相关文章

  1. 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 ...

  2. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  3. 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 ...

  4. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  6. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

  7. 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 ...

  8. 动态规划法(三)子集和问题(Subset sum problem)

      继续讲故事~~   上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界.   这天,丁丁刚回到家,他 ...

  9. HDU 2058:The sum problem(数学)

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 【react学习】关于react框架使用的一些细节要点的思考

    ( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的)   这篇文章主要是写关于学习react中的一些自己的思考:   1.set ...

  2. Git命令行和Xcode结合使用

    现在一直使用Git来管理代码,对于有强迫症的我来说,依旧选择了命令行,下面这段话可以更好的解释我为什么喜欢使用终端敲命令. There are a lot of different ways to u ...

  3. 数据库Schema两种含义~~

    1.数据库Schema有两种含义,一种是概念上的Schema,指的是一组DDL语句集,该语句集完整地描述了数据库的结构.还有一种是物理上的 Schema,指的是数据库中的一个名字空间,它包含一组表.视 ...

  4. oracle调用array参数存储过程

    declare -- Non-scalar parameters require additional processing files tyt_gas2014_search; ,); temp1 t ...

  5. 本学期微分方程数值解课程总结(matlab代码)

    最简单求解一个微分方程数值解得方法:Euler法 function [x,y]=Euler_method(dufun,span,h,x0,y0) %EuLer格式, %求解方程y'=dufun(x,y ...

  6. three.js提供的几何体

    1.简单几何体 three.js提供的稍微简单点的几何体包括有:PlaneGeometry(平面).CircleGeometry(圆形).ShapeGeometry(塑性).CubeGeometry( ...

  7. CSAcademy Beta Round #4 Swap Pairing

    题目链接:https://csacademy.com/contest/arhiva/#task/swap_pairing/ 大意是给2*n个包含n种数字,每种数字出现恰好2次的数列,每一步操作可以交换 ...

  8. Xamarin自定义布局系列——支持无限滚动的自动轮播视图CarouselView

    背景简述 自动轮播视图(CarouselView)在现在App中的地位不言而喻,绝大多数的App中都有类似的视图,无论是WebApp还是Native App.在安卓.iOS以及Windows(UWP) ...

  9. hibernate 使用 hibernate tool 生成配置文件和实体类

    安装Hibernate插件 下载所需的Hibernatetools  http://www.hibernate.org/6.html 将下载得到的文件解压得到的features和plugins文件夹, ...

  10. smart beta

    本文来至人大经济论坛,http://bbs.pinggu.org/thread-3151691-1-1.html 众所周知,beta在CAPM模型中衡量了相对于持有整个市场所带来的风险溢价(risk ...