ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1068. Sum

accepted pure c code
Posted by Иван 8 May 2014 21:11
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int n, tmp;
    long sum;
    scanf("%d", &n);
    if (n > 10000 || n < -10000) {
        printf("%d\n", 0);
        return 0;
    }

    if (n == 0) {
        printf("%d\n", 1);
        return 0;
    }
    if (n > 0) { /* positive */
        if (n % 2 == 0) { /* even */
            sum = n * (n / 2 - 1) + n + (n / 2);
        } else { /* not even */
            sum = n * (n / 2) + n;
        }
    } else { /* negative */
        if (n % 2 == 0) { /* even */
            sum = n * (n / 2 + 1) - n - (n / 2) - 1;
            tmp = ~sum + 1;
            sum = tmp;
        } else { /* not even */
            sum = n * (n / 2) - n - 1;
            tmp = ~sum + 1;
            sum = tmp;
        }
    }

    printf("%ld\n", sum);
    return 0;
}

/* n * (n / 2) + n not even */
/* n * ((n / 2) - 1) + n + (n / 2) even */
/* n * (n / 2) - n - 1 negative not even */
/* n * ((n / 2) + 1) - n - (n / 2) - 1 negative even */
Re: accepted pure c code
Posted by Nafiul Islam 4 Oct 2016 22:29
#include<stdio.h>
int main(){
    int n,N,ans;
    scanf("%d",&N);
    if(N > 1){
        n=N;
    }else{
        n=-1*N+2;
    }
    ans=(n*(N+1))/2;
    printf("%d\n",ans);
    return 0;

}

Also accepted