NYOJ 46 sum of all integer numbers

来源:互联网 发布:留学cv 知乎 编辑:程序博客网 时间:2024/05/23 01:02

sum of all integer numbers

时间限制:1000 ms  |  内存限制:65535 KB
难度:0
描述
Your task is to find the sum of all integer numbers lying between 1 and N inclusive.
输入
There are multiple test cases.
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
样例输出
6


大意是:输入一个N,(可正可负),输出1-N的和 

需要注意的是:如果N是负数就输出N-1的和


#include<stdio.h>#include<stdlib.h> int main(){  int n;  while(scanf("%d", &n) != EOF){    if(n < 1)      printf("%d\n", (1+n)*(abs(n)+2)/2);//负数情况,项数得加上2     else      printf("%d\n", (1+n)*n/2);  }} 



原创粉丝点击