Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
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.
水题,直接贴代码吧
#include<iostream>
using namespace std;
int sum(int n)
{
int sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+i;
}
return sum;
}
int main()
{
int n;
while(cin>>n)
{
cout<<sum(n)<<endl<<endl; //按照题目要求,换行符一定要2个,否则会presentation error
}
return 0;
}