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

where is wrong !! already it is working in codeblocks
Posted by EIASER ARAFAT 6 Nov 2017 16:02
#include<stdio.h>

int main(){
int n,sum=0,i;
scanf("%d",&n);-

if(n>0){
  sum=(n*(n+1))/2;
}
else if(n<0){
        sum=(n*(-1*n+1))/2;
}
printf("%d",sum);


}
Re: where is wrong !! already it is working in codeblocks
Posted by Mahilewets Nikita 6 Nov 2017 19:46
Maybe for N < 0 the formula is
SUM = ((2-N)*(N+1))/2

Edited by author 06.11.2017 19:47
Re: where is wrong !! already it is working in codeblocks
Posted by Mahilewets Nikita 6 Nov 2017 19:55
The first term is N
The last term is 1
So the average is (1+N)/2

There are (-N) terms below zero
And there are two more terms
They are 0 and one
So there are (2-N) total terms

So the sum is average by total number of terms
Re: where is wrong !! already it is working in codeblocks
Posted by ToadMonster 7 Nov 2017 01:19
Case "N == 0" isn't processed
Re: where is wrong !! already it is working in codeblocks
Posted by jim 22 Feb 2018 07:10
thank you for your hint!
Re: where is wrong !! already it is working in codeblocks
Posted by WeXoB 20 May 2020 08:51

Edited by author 20.05.2020 08:52

Edited by author 20.05.2020 08:52