URAL 1068. Sum (小学数学题)

来源:互联网 发布:plc编程器 编辑:程序博客网 时间:2024/05/02 00:34

1068. Sum

Time limit: 2.0 second
Memory 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




题意:给一个整数N,求1~N的连续和。

解析:N可正可负。



AC代码:

#include <cstdio>int main(){    int n, sum;    while(scanf("%d", &n)==1){        sum = 0;        if(n < 1) sum = n*(1 - n)/2 + 1;        else sum = n*(n+1)/2;        printf("%d\n", sum);    }    return 0;}


0 0
原创粉丝点击