Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 224457 Accepted Submission(s): 55071

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   水题,不过有个陷阱,坑了我好久啊,一个边界溢出问题,要注意。 下面是AC代码: 用这种方法写是没什么问题的,注意用long类型:
#include<stdio.h>
int main ()
{
long int n,i,sum;
while(scanf("%ld",&n)!=EOF)
{
sum=0;
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("%ld\n",sum);
printf("\n");
}
return 0;
}

但是如果用求和公式的话,一定要这样,防止边界溢出,考虑项数除以2 是否为0

#include<iostream>
using namespace std;
int main()
{
int s,n;
while(cin>>n)
{
s=(n%2==0?n/2*(n+1):(n+1)/2*n);
cout<<s<<endl<<endl;
}
}

java版就大的多

import java.util.*;
public class Main {
public static void main(String[] args)
{
int sum,i,n;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
sum =0;
n=cin.nextInt();
for(i=0;i<=n;i++)
{sum+=i;}
System.out.println(sum);
System.out.print("\n\n");
}
}
}

1001Sum Problem的更多相关文章

  1. HDOJ1001-1005题解

    1001--Sum Problem(http://acm.hdu.edu.cn/showproblem.php?pid=1001) #include <stdio.h> int sum(i ...

  2. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  3. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  4. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  5. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  6. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  7. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  8. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  9. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

随机推荐

  1. 转:ViewPager+Fragment基本使用方法(附源码)

    ViewPager+Fragment可以做出多页面滑动效果,让我们的应用程序界面操作起来更加灵活 对于ViewPager和Fragment组件还不熟悉的朋友,可以先看看相关的资料 首先在activit ...

  2. HDU 1513 Palindrome

    题目就是给一个字符串问最少插入多少个字符能让原字符串变为回文字符串. 算法: 用原串的长度减去原串与翻转后的串的最大公共字串的长度,就是所求答案. //#define LOCAL #include & ...

  3. Windows下PhpStorm结合WAMP开发Phalcon应用的配置

    最近要利用Phalcon框架开发PHP应用,因为以前基本没接触过PHP更没用过PHP框架,结果整环境整IDE配置什么的花了好长时间 学习慕课网上的PHP入门教程安装了WAMP(windows+apac ...

  4. 850 USB 烧录模式

    /************************************************************************* * 850 USB 烧录模式 * 说明: * 本文 ...

  5. erl0010 - erlang查看ets 当前系统使用情况和当前配置上限

    1.限制:erlang官网给出了ets的默认上限:“The default is 1400, can be changed with the environment variable ERL_MAX_ ...

  6. mac vim shell配置

    一 : vim 配置 1 目录/usr/share/vim/vimrc 2 Python 自动缩进 http://blog.csdn.net/ikerpeng/article/details/1866 ...

  7. 【英语】Bingo口语笔记(72) - play系列

  8. 【Python】类和对象、继承、使用文件、存储、异常、标准库(不懂)

    当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self ...

  9. JBPM4入门——7.等待节点的单条线手动执行

    本博文只是简要对JBPM4进行介绍,如需更详细内容请自行google 链接: JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流 ...

  10. SVN中检出(check out) 和 导出(export) 的区别

    SVN是常用的一种常见的版本控制软件.SVN中检出(check out) 和 导出(export) 的区别主要有如下几条: check out跟check in对应,export跟import对应. ...