Spit Groove ~~~~~~~~~~~~~

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1001

/(ㄒoㄒ)/~~ originally I was so weak, it is just unexpected don't know, a test will frighten the baby/(ㄒoㄒ)/~~

Submitted 10 times, is enough, has not been aware of the problem is the overflow problem, there is the odd and even problem

There are three ways of doing it, actually two kinds.

1#include <stdio.h>2 3 intMain ()4 5 {6 7     intI,n;__int64 sum;//it is necessary to set the type of sum to __int64, otherwise it will overflow. 8 9      while(SCANF ("%d", &n)! =EOF)Ten  One { Asum=0; -  -        for(i=1; i<=n;i++) the  -sum+=i; -  -printf"%i64d\n\n", sum); +  -   +  A } at  - return 0; -  -}

Second:

1#include <stdio.h>2 intMain ()3 {4     intN;5 __int64 sum;6      while(scanf_s ("%d", &n)! =EOF)7     {8sum =0;9         if(n%2==0)Tensum = (N/2) * (n +1); One         Else A  -sum = (1+ N)/2*N; -printf"%i64d\n\n", sum); the              -     } -     return 0; -}

The third is to use a floating-point output for sum so that you don't have to discuss the odd-even ~~~~~~~ about overflow, is first in addition to 2 multiply, so as to prevent overflow, the first time with __int64, it is absolutely awesome, overflow problem or to pay attention to, because otherwise ...... ..... ..... ......... The consequences are serious. 2333333333333333

Spit Groove ~~~~~~~~~~~~~

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.