题目描述
Your task is to find the sum of all integer numbers lying between 1 and
N
inclusive.
输入
The input consists of a single integer
N that is not greater than 10000 by it's absolute value.
输出
Write a single integer number that is the sum of all integer numbers lying between 1 and
N
inclusive.
样例输入
-3
样例输出
-5
提示
注意:是【1,n】||【n,1】,不是(i,n)||(n.1);
考虑两种情况,n<=0,n>0;
题解:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
if(n>10000)
return 0;
else
{
int add=0;
if(n>0)
for(int i=1;i<=n;i++)
{
add=add+i;
}
else
if(n<=0)
{
for(int i=n;i<2;i++)
{
add=add+i;
}
}
cout<<add;
}
}