HDU 1001 Sum Problem 2019.3.21.2
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
#include<iostream>
using namespace std;
int main()
{
int n,i;
while(cin>>n){
int sum=0;
for(i=1;i<=n;i++){
sum+=i;
}
cout<<sum<<endl;
cout<<endl;
}
return 0;
}
总结
这道题有三个地方需要注意。
第一,sum=0的位置是放在while循环里面,还是放在外面?
第二,输出格式里有一行空白,怎么实现空白行的输出呢?
第三,空白行的位置是放在sum结果后,还是放在结果之前呢?