【HDU】-1001-Sum Problem(水,变态)

    xiaoxiao2025-08-21  10

    Sum Problem

    Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 417386    Accepted Submission(s): 105189 Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.   Input The input will consist of a series of integers n, one integer per line.   Output For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.   Sample Input 1 100   Sample Output 1 5050   Author DOOM III   题解:说是水题吧,就是A不了。 看了别人博客才知道就是n*(n+1)/2答案是在范围内的,但是在n*(n+1)这里会溢出,分奇偶判断就好了。

    #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; #define CLR(a,b) memset(a,b,sizeof(a)) int main() { int n; while(~scanf("%d",&n)) { if(n&1) printf("%d\n\n",(n+1)/2*n); else printf("%d\n\n",n/2*(n+1)); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1301895.html
    最新回复(0)