Your task is to find the sum of all integer numbers lying between 1 and Ninclusive.
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 N inclusive.
Example
| input | output |
|---|---|
|
|
#include <stdio.h>
int main(){
int n, sum;
while(~scanf("%d", &n)){
sum = 0;
if(n < 1)
sum = n*(1-n)/2 + 1;
else sum = n*(n+1)/2;
printf("%d\n", sum);
}
return 0;
}

本文介绍了一个简单的算法,用于计算从1到N(包含N)的所有整数的总和。输入为一个整数N,输出为1到N的整数之和。算法区分了N大于等于1和小于1的情况,分别使用了不同的计算公式。
1万+

被折叠的 条评论
为什么被折叠?



