Ural-1068. Sum(水题)

来源:互联网 发布:电脑便签软件 编辑:程序博客网 时间:2024/05/22 14:11

1068. Sum

Time limit: 2.0 secondMemory limit: 64 MB
Your task is to find the sum of all integer numbers lying between 1 and N inclusive.

Input

The input consists of a single integer N that is not greater than 10000 by it's absolute value.

Output

Write a single integer number that is the sum of all integer numbers lying between 1 and Ninclusive.

Sample

inputoutput
-3
-5


题意:计算1-N的和
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <map>#include <set>#include <algorithm>using namespace std;int n,sum;int main(){//    freopen("in.txt","r",stdin);    while(scanf("%d",&n)!=EOF)    {        sum = 0;        if(n<0)        {            sum = (n-1)*(-n)/2;            sum++;        }        else if(n>1)        {            sum = (n+1)*n/2;        }else        {            sum = 1;        }        printf("%d\n",sum);    }    return 0;}