HDU Sum Problem (一道神坑的水题)

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
 
巨坑无比 
我直接用等差公式 WA到根本停不下来
 
题目中标红的字是关键 
n*(n+1)可能会超出32位
 
所以先 n/2(n%2==0)
或者(n+1)/2  ---(n%2==1)
 
代码自己写吧

转载于:https://www.cnblogs.com/whistle13326/p/7147730.html

你可能感兴趣的:(HDU Sum Problem (一道神坑的水题))