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. 打印Fibonacci数列方法汇总(前20项,每行5个)

    NO.1 迭代法 标签:通俗.易懂 思路:先打印第一项.再在循环里面执行fib=fib1+fib2,把fib2赋给fib1,把fib赋给fib2,每行5个可使用if函数(循环次数对5取余). #inc ...

  2. JQuery事件与动画总结

    1.加载DOM 1.1.window事件 window.onload=function(){}.... 时机:其他资源都加载完毕后,再执行 $(function(){}) ……:只是等待标签完毕,即可 ...

  3. Linq 查询与普通查询的区别

    普通:select * --1 from User(表名) as u --2 where u.Name like '%s%' --3 Linq : from User(表名) as u --1 whe ...

  4. linux编译安装php7

    1.首先下载php7 使用wget命令下载 wget http://cn2.php.net/distributions/php-7.0.12.tar.bz2 2.然后解压 tar -xvf php-7 ...

  5. nginx参数的详细说明

    #开启进程数 <=CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #error_log logs/error.log ...

  6. 把上传Github的代码添加Cocoapods支持

    开始 这里我将从最初的开始进行介绍,包括Github上创建项目已经上传项目,到最后的支持Cocoapods. 步骤如下: 代码上传Github 创建podspec文件,并验证是否通过 在Github上 ...

  7. 使用Express开发个人网站(一)

    Express,基于Node.js平台,快速.开放.极简的 web 开发框架. Node的出现,让js有了运行在服务器端的可能,基于此的Express,可以快速,简单的搭建起一个服务器与个人网站. 安 ...

  8. 学好php可以做的事情真多!

    学好php能做什么?其实学好php能做的事情很多! 一. 学好php可以就业 1:大中小公司通吃. 现在几乎所有有前途的公司都会在互联网上安家.只要在网上安家,就需要找这些方面的技术人员,而且很多公司 ...

  9. time元素定义的格式

    time元素表示的是24小时中的某一个时刻或日期,表示时间时允许时差. time元素定义的格式如下: <time datetime="2016-6-15">2016年6 ...

  10. GitHub的实践

    GitHub的实践 2017-05-08,晴,来小米已经一周的时间了,感谢领导能给我一周的时间来熟悉 ubuntu.spring boot.maven.docker.github .大家会问,这些不都 ...