Problem Description
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
 ------------------------------------------------------------------------------------------------------------------
Sn = n*(n+1)/2
把两个相同的自然数列逆序相加
2Sn=1+n + 2+(n-1) + 3+(n-2) + ... n+1
=n+1 +n+1 + ... +n+1
=n*(n+1)
Sn=n*(n+1)/2
另,
m到n的自然数之和:Smn=(n-m+1)/2*(m+n)
(n>m)
Smn=Sn-S(m-1)
=n*(n+1)/2 -(m-1)*(m-1+1)/2
={n*(n+1) - m(m-1)}/2
={n*(n+1) - mn + m(1-m) + mn }/2
={n*(n-m+1)+ m(1+ n-m)}/2
=(n+m)(n-m+1)/2
注意:虽然题目说sum不会大于32位,但是n*(n+1)会大于32位。
 
#include <stdio.h>
#include <stdlib.h> int main()
{
long long n;
//freopen("F:\\input.txt", "r", stdin);
while (scanf("%lld", &n) != EOF)
{
printf("%lld\n\n", n * (n + 1) / 2);
}
//freopen("CON", "r", stdin);
//system("pause");
return 0;
}

  

ACM1001:Sum Problem的更多相关文章

  1. Problem-1001:Sum Problem

    Sum Problem Sample code : #include <stdio.h> int main() { int i,n; int sum; while(scanf(" ...

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

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

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

  4. HD2058The sum problem

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

  5. ACdream: Sum

    Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticN ...

  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. 动态规划法(三)子集和问题(Subset sum problem)

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

  8. NYOJ 927 The partial sum problem 【DFS】+【剪枝】

    The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...

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

随机推荐

  1. Android深入理解Context(二)Activity和Service的Context创建过程

    前言 上一篇文章我们学习了Context关联类和Application Context的创建过程,这一篇我们接着来学习Activity和Service的Context创建过程.需要注意的是,本篇的知识 ...

  2. node(6)angular介绍

    一.angular 的介绍 AngularJS[1]  诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中. ...

  3. Azure 和 Linux

    Azure 正在不断集结各种集成的公有云服务,包括分析.虚拟机.数据库.移动.网络.存储和 Web,因此很适合用于托管解决方案. Azure 提供可缩放的计算平台,允许即用即付,而无需投资购买本地硬件 ...

  4. .net网站转到出错页是如何实现的

    <customErrors mode="On" defaultRedirect="GenericErrorPage.htm"><error s ...

  5. 记录Ubuntu14.04 LTS版本中使用Docker的过程

    sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...

  6. IIS6与IIS7中的w3wp工作进程

    在IIS6中,每一个网站都有对应的应用程序池,在应用程序池有运行着网站的Application,在默认情况下,所有的网站的应用程序都会分配到默认的应用程序池当中,   当然,我们可以新建一个应用程序池 ...

  7. September 26th 2017 Week 39th Tuesday

    I have to protect the one thing I can't live without. 我必须为我一生挚爱遮风挡雨. A man is a success if he gets u ...

  8. 16:42 python历史

    python的作者是Guido van Rossum,大牛,2.7版本好像到2020年就不能用了,估计很多公司对此有很多的需求吧.

  9. Android 高级UI设计笔记24:Android 夜间模式之 WebView 实现白天 / 夜间阅读模式 (使用JavaScript)

    1. 问题引入: 前面我们是使用方法 降低屏幕亮度(不常用) 和 替换theme,两者都是针对Activity的背景进行白天.夜间模式的交换,但是如果我们显示的是Html的内容,这个时候改怎么办? 分 ...

  10. python统计磁盘使用情况

    #coding:utf-8import os; def SortList(item):    return item[1]; def ReadSize(fileName):    return flo ...