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 ~~~~~~~~~~~~~