NYOJ-sum of all integer numbers

Source: Internet
Author: User
Tags integer numbers

NYOJ-sum of all integer numbers
Sum of all integer numbers time limit: 1000 MS | memory limit: 65535 KB difficulty: 0

Description
Your task is to find the sum of all integer numbers lying between 1 and NIntrusive.
Input
There are multiple test cases.
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 random Sive.
Sample Input
3
Sample output
6

Code:

#include
     
      #include
      
       int main(){int n;while(~scanf("%d",&n)){if(n==0){printf("1\n");continue;}int k=1,t=0;  if(n<0)k=-1,t=1;n=abs(n);if(n&1)printf("%d\n",((n+1)>>1)*n*k+t);elseprintf("%d\n",(n>>1)*(n+1)*k+t);   }return 0;}
      
     


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.