1068. Sum
Time limit: 2.0 second
Memory limit: 64 MB
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
input | output |
---|---|
-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;
}