HDU 1001

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

import java.util.*;
class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        int n = sc.nextInt();
        int sum = 0;
        for(int i =1;i<=n;i++){
          sum = sum + i;
           }    
    System.out.println(sum);
    System.out.println( );
  /*  if(n==0)
                break;*/
    }
  }
}
阅读更多
版权声明:本文为博主原创文章,大家一起学习进步。 https://blog.csdn.net/weixin_37720904/article/details/60140816
文章标签: acm
个人分类: hdu--ACM
上一篇HDU 1000 ACM开始之路
下一篇HDU 1089
想对作者说点什么? 我来说一句

动态规划背包问题入门

2011年07月07日 328KB 下载

没有更多推荐了,返回首页

关闭
关闭