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

Help !!How simple the (1068 Sum) is,but I'm wrong,Why?
Posted by Song Chao (ECUST Mutistar) 9 Feb 2002 07:17
Var N,i,S,Sign:longint;

begin
  readln(N);
  if N>0 then Sign:=1 else Sign:=-1;
  N:=Abs(N);
  S:=Sign*(2+N)*(N-1) div 2 ;
  writeln(S);
  readln;
end.
I'm fool,I misunderstood the problem.
Posted by Song Chao (ECUST Mutistar) 9 Feb 2002 07:44
> Var N,i,S,Sign:longint;
>
> begin
>   readln(N);
>   if N>0 then Sign:=1 else Sign:=-1;
>   N:=Abs(N);
>   S:=Sign*(2+N)*(N-1) div 2 ;
>   writeln(S);
>   readln;
> end.

The Correct Program:

Var N,i,S,Sign:longint;

begin
  readln(N);
  if N>0 then Sign:=1 else begin N:=-N; Sign:=-1 end;
  S:=sign*(1+N)*N div 2;
  if sign<0 then S:=S+1;
  writeln(S);
  readln;
end.