HDOJ_1001_Sum Problem

来源:互联网 发布:android仿淘宝头条 编辑:程序博客网 时间:2024/05/22 01:57

题目: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


这个题目逻辑、编程都是没有什么问题的,但是有几个小坑。

1.要看清输出格式,每一行下面需要空一行,还有就是

2.n的类型,应为double,我一开始是int型,怎么也不能AC。


代码如下:

#include<iostream>using namespace std;int main(){    double n;    while(cin>>n){        cout<<(int)((1+n)*n/2)<<endl<<endl;    }}
原创粉丝点击