|
|
back to boardDiscussion of Problem 1068. SumWhy dosn't work my program on the easiest problem? I wrote a program for 1068 "Sum" - the code is really very easy. Judge program says always me "wrong answer". Where is my mistake? Could anybody help me? program p; var i,n:longint; res:longint; begin read(n); res := 0; if (n >=1) then for i:=1 to n do res := res + i else for i:=1 downto n do res := res + i; write(res); end. Something's wrong with downto there! I just took your solution and changed loop downto a little. This will be accepted: (has already been:). Mind that you don't have to use loop at all - just using formula n*(n+1)/2 seperately for n>=1 and n<1 :) I can post my solution here if you want, or send it by email program p; var i,n:longint; res:longint; begin read(n); res := 0; if (n >=1) then for i:=1 to n do res := res + i else begin res:=1; for i:=1 to -n do res := res - i; end; write(res); end. |
|
|