杭电ACM 1001
2016-05-04 19:02
330 查看
Sum Problem
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 406156 Accepted Submission(s): 102002
[align=left]Problem Description[/align]
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
[align=left]Input[/align]
The input will consist of a series of integers n, one integer per line.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
100
[align=left]Sample Output[/align]
1
5050
#include<iostream>
using namespace std;
int main(){
int a;
while(cin>>a){
if(0==a%2)///防止溢出
cout<<(a/2*(a+1))<<endl<<endl;
else
cout<<(a+1)/2*a<<endl<<endl;
}
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002
- 高精度加法——杭电1002