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

Show all messages Hide all messages

I got AC with an 28K program!! Drizzt 15 Mar 2003 08:08
program Sum (input, output);
  var lI, lN : longint;
  begin
    readln (lN);
    if lN > 0 then lI := (lN + 1) * lN div 2;
    if lN = 0 then lI := 1;
    if lN < 0 then lI := (abs (lN) + 1) * lN div 2 + 1;
    writeln (lI);
  end.
Var
  n                   :integer;
  sum                 :longint;
begin
  readln(n);
  if n=0 then n:=1;
  sum:=abs(n)*(abs(n)+1) div 2;
  if n<0 then sum:=1-sum;
  writeln(sum);
  readln;
end.
~~~~~~~~~~~~~~
u used 2 Longint (2*4b) and mine 1longint (4) + a Integer (2)
so 4+2<2*4 !!! :P
Please Stop sending AC dear,
Best Regards
Aidin_n7
How about my programme.
var
 n:longint;
begin
  readln(n);
  writeln((1+n)*(abs(n-1)+1)div 2);
end.

Edited by author 18.02.2006 15:12

Edited by author 18.02.2006 15:12
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int main (void) {
    int n;
    scanf("%d", &n);
    printf("%d\n", ((1 + n) * (abs(1 - n) + 1)) >> 1);
    return 0;
}